[IOS development]-Multi-controller jump, ios development controller jump
Requirement 1: It is impossible for us to create an APP with one interface to achieve all the requirements. We need to jump to multiple interfaces. Each interface implements its own needs. Code redirection between multiple controllers is roughly divided into three types. One is to use UINavigationController to redirect, the other is to use UITabBarController to redirect between multiple controllers, and the other is to use presentViewController (UIViewController, animated: animated
, Completion: completion.
1. UINavigationController:
Achieve redirection: Use UINavigationController to set the root controller. The subsequent redirection is based on the Root controller. The code for setting the root controller is as follows:
// Set the root Controller
Let nav = UINavigationController (rootViewController: ViewController ())
* When Using UINavigationController, push and pop are used for redirection. The Code is as follows:
NavigationController. pushViewController (Target Controller, animated: true/false)
NavigationController. popToViewController (roll back to the previous controller, true/false)
NavigationController. popToRootViewControllerAnimated (true/false)
* Note that push is redirected to the target controller, pop is rolled back to the previous controller, and popToRootView is directly rolled back to the root controller.
* When you use UINavigationController to redirect, UINavigationController sets the "navigation bar" for all target controllers. After the push is completed, a return item is automatically generated on the "navigation bar, click return item and the pop method will be executed to return to the previous controller.
Conclusion: When you use UINavigationController to redirect, you only need to set the appearance of UINavigationController once. The derived "navigation bar" will maintain the appearance of the root controller. To set the appearance, you must set the appearance proxy object (appearance) in the initialize method. Note that initialize is a class method.
2. UITabBarController:
Achieve redirection: Use UITabBarController. Note that you must set all controllers to be redirected at the same time. The sub-controller of UITabBarController exists at the same time. Other Sub-controllers are not released when the target sub-controller is redirected. You can set the sub-controller by addChildViewController: ViewController. This method can only set one sub-controller at a time. You can use viewControllers to set multiple subcontrollers. viewControllers returns an array. When using viewControllers, you can create a controller, put the controller into an array, and assign values directly. The order in the array is the order in which tabBar displays the controller. The Code is as follows:
1). Use addChildViewController
Self. addChildViewController: ViewController1 ()
Self. addChildViewController: ViewController2 ()
Self. addChildViewController: ViewController3 ()
2). Use viewControllers
Self. viewControllers = [ViewController1 (), ViewController2 (), ViewController3 ()]
* During the jump, click the barButtonItem on the tab bar.
3. presentViewController:
Achieve redirection: You can use presentViewController to redirect to any controller. However, after the redirection is completed, you must complete the dismiss method if you want to return the result. The jump feature of presentViewController is that the jump from the bottom of the screen to cover the entire screen, the effect is still quite good, it is worth noting that after the dismiss method is completed, presentViewController will be released from the memory. The Code is as follows:
PresentViewController (ViewController1 (), animated: true/false, completion: nil)
* Completion is the callback executed after the jump is completed. We generally fill in nil.
* You must first write the dismiss method after completing the first hop.
DismissViewControllerAnimated (true/false, completion: nil)
* This completes the return and the controller is released from the Controller.
Conclusion: This completes the jump between multiple interfaces.
Requirement 2: Of course, you can also use sb to redirect between controllers. It is easy to use sb to drag the show line directly.