[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. Concept1. generally, an app has multiple controllers. need to manage these controllers 3. when multiple views exist, use one parent view to manage multiple child views. the same is true for Controller Management. A parent controller is used to control sub-controllers. To facilitate the management of controllers, the system provides two controllers.

  • UINavigationController
  • UITabBarController
B. Use of UINavigationControllerHow to Use UINavigationController
Initialize UINavigationController
Set the rootViewController of UIWindow to UINavigationController. Add the corresponding number of sub-controllers through the push method according to the actual situation.  1. initialize 2. Set as rootViewController
1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // set window 3 self. window = [[UIWindow alloc] init]; 4 self. window. frame = [[UIScreen mainScreen] bounds]; 5 self. window. backgroundColor = [UIColor grayColor]; // The window is a gray background 6 [self. window makeKeyAndVisible]; 7 8 9 // set UINavigationController10 UINavigationController * nvController = [[UINavigationController alloc] init]; 11 nvController. view. backgroundColor = [UIColor blueColor]; // set the blue background 12 self. window. rootViewController = nvController; 13 14 return YES; 15}
3. Add the sub-controller (1) by using the push method to create three controllers and the bound xib file (2) push them to the sub-controller stack of navigationController at a time.
1 // configure the first sub-controller 2 ViewControllerOne * vc1 = [[ViewControllerOne alloc] init]; 3 [nvController pushViewController: vc1 animated: YES]; 4 5 // configure the second subcontroller 6 ViewControllerTwo * vc2 = [[ViewControllerTwo alloc] init]; 7 [nvController pushViewController: vc2 animated: YES]; 8 9 // configure the third subcontroller, which is the Controller 10 ViewControllerThree * vc3 = [[ViewControllerThree alloc] init]; 11 [nvController pushViewController: vc3 animated: YES];
(3) view of the stack top Controller C. Management Principles of sub-Controllers1. The navigation bar is in the status bar (height 20) and the height is 44.
1-(void) applicationDidBecomeActive :( UIApplication *) application {2 // the size and position of all views must be determined after the app is loaded and activated. 3 UINavigationController * nvController = (UINavigationController *) self. window. rootViewController; 4 NSLog (@ "% @", NSStringFromCGRect (nvController. view. frame); // The frame5 NSLog (@ "% @", NSStringFromCGRect (nvController. navigationBar. frame); // frame 6} 7 of the navigation bar
Out: NavigationController [2669: 183137] {0, 0}, {320,480 }} 21:16:30. 413 NavigationController [2669: 183137] {0, 20}, {320, 44 }}2. UINavigationController has a sub-controller stack. The push method adds a sub-controller to the stack.
1 @property(nonatomic,copy) NSArray *viewControllers; // The current view controller stack.
3. When the sub-controller is pushed, the view on the window is replaced, and the view of the stack top controller is always displayed.
1 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
  D. Sub-controller stack operations1. Stack and add sub-controllers (1) Use "pushViewController"
1 // configure the first sub-controller 2 ViewControllerOne * vc1 = [[ViewControllerOne alloc] init]; 3 [nvController pushViewController: vc1 animated: YES];
(2) Use "addChildController"
  • The "viewControllers" member in UINavigationController is an array, that is, the sub-controller stack.
  • The same is true for "childViewControllers" (this is a member attribute of UIViewController)
  • "AddChildController" and "pushViewController" provide the same functions (this is the method of UIViewController)
// Configure the first subcontroller
1     ViewControllerOne *vc1 = [[ViewControllerOne alloc] init];2     [nvController addChildViewController:vc1];
(3) directly add the viewControllers Array
1 // configure the first sub-controller 2 ViewControllerOne * vc1 = [[ViewControllerOne alloc] init]; 3 nvController. viewControllers = @ [vc1];
(4) specify rootViewController during initialization
1 // pass in a viewController as the rootViewController2 nvController of nvController = [[UINavigationController alloc] initWithRootViewController: vc1];
  Note: Do not mix "pushViewController" and "addChildViewController". In some cases, the controller is not added to the stack. The results are also messy and unpredictable.(5) use events for inbound stack. add two buttons to the view of the second controller to jump to view B of the third and fourth controllers respectively. click Event method each controller has a navigationController attribute to notify navigationController to perform operations.
1-(IBAction) goToThree {2 // configure the third sub-controller 3 ViewControllerThree * vc3 = [[ViewControllerThree alloc] init]; 4 [self. navigationController pushViewController: vc3 animated: YES]; 5} 6 7-(IBAction) gotoFour {8 // configure the fourth sub-controller 9 ViewControllerFour * vc4 = [[ViewControllerFour alloc] init]; 10 [self. navigationController pushViewController: vc4 animated: YES]; 11}
2. Activate the stack (1) Use "popViewControllerAnimated: YES" to bring up a controller
1 // here is the third one. Return to the second 2-(IBAction) backtoTwo {3 [self. navigationController popViewControllerAnimated: YES]; 4}
(2) Use "popToViewController" to bring up the Controller until the specified controller (3) use "popToRootViewController" to bring up the Controller that occupies the inner until rootViewController is returned to the Controller at the bottom of the stack.
1 // view of the third controller. Return to view2-(IBAction) of the first controller) backtoOne {3 // In fact, the reference of the first controller vc1 is not available here. 4 // [self. navigationController popToViewController: vc1 animated: YES]; 5 6 // directly pops up until the rootViewController of the navigator is the first subcontroller vc1 with 7 [self. navigationController popToRootViewControllerAnimated: YES]; 8}
  E. Set the navigation bar1. The navigation bar content is controlled by the stack top controller 2. Use navigationItem to control the navigation bar content (1) title/titleView (this is a member attribute of UIViewController)
1 @property(nonatomic,copy) NSString *title;  // Localized title for use by a parent controller.
Sample: Set the title of the first controller.
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 self. navigationItem. title = @ "first view"; 4}
(2) leftBarButtonItem/leftBarButtonItems will overwrite the "return" button (3) rightBarButtonItem/rightBarButtonItems
1-(void) viewDidLoad {2 [super viewDidLoad]; 3 self. navigationItem. title = @ "second controller"; 4 5 // set item 6 self in the upper left corner. navigationItem. leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "I Want To Go Back" style: UIBarButtonItemStylePlain target: nil action: nil]; 7 8 // set item 9 in the upper-right corner // use bar button 10 UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCompose target: nil action: nil]; 11 12 // use custom view13 UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithCustomView: [UIButton buttonWithType: UIButtonTypeContactAdd]; 14 15 // This is where 16 self is displayed from right to left. navigationItem. rightBarButtonItems = @ [item1, item2]; 17}
Note: The leftBarButtonItems is displayed from left to right in the array order, while the rightBarButtonItems is displayed from right to left. For example, place the items on the right to the left: self. navigationItem. leftBarButtonItems = @ [item1, item2]; 3. if leftBarButtonItem (s) is not overwritten ), the text of the "return" button is automatically replaced with the title of the previous level --> the text displayed in the upper left corner is the navigationItem controlled by the Controller of the previous level. backBarButtonItem
1 // second controller 2-(void) viewDidLoad {3 [super viewDidLoad]; 4... 5 // set the "return" style of the second controller 6 self. navigationItem. backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle: @ "2 2" style: UIBarButtonItemStylePlain target: nil action: nil]; 7}
4. navigatonBar is the entire navigation bar nvController. navigationBar. After all the items are loaded, the frame of the navigation bar can be initialized.
1-(void) applicationDidBecomeActive :( UIApplication *) application {2 // the size and position of all views must be determined after the app is loaded and activated. 3 UINavigationController * nvController = (UINavigationController *) self. window. rootViewController; 4 NSLog (@ "% @", NSStringFromCGRect (nvController. view. frame); // The frame5 NSLog (@ "% @", NSStringFromCGRect (nvController. navigationBar. frame); // frame6 of the navigation bar}
Out: 23:12:51. 049 NavigationController [4214: 236071] {0, 0 },{ 320,480 }} 23:12:51. 050 NavigationController [4214: 236071] {0, 20}, {320, 44 }}

Last click link:

  F. Use storyboard navigation1. create a basic navigation interface (1) create a single view application, delete the built-in ViewController class (2) Delete the original ViewController, drag a NavigationController, and set it to Initial View Controller (3) to delete the built-in rootViewController, drag a UIViewController (4) and set the newly dragged UIViewController to set the UIView for the rootViewController (5) of the navigator, this means that the text (such as title) can be edited by double-clicking the corresponding position on the first page, but the BarButtonItems group cannot be dragged using the storyboard method. (6) drag a UIView to UIViewController and add some elements for running effect: 2. create jump navigation function (1) Add two jump buttons to the view of the first controller, drag two UIViewController (2) drag to point, use the "push" event to achieve the effect: in this way, you can jump to the second or third Controller Interface on the first Controller Interface. Note: Do not create a button to navigate back. Instead, create a new controller and add it to the Controller stack. G. lifecycle of the Controller1. lifecycle method 2. Memory warning processing process when the memory is insufficient, the view that does not exist on the screen should be recycled first. Therefore, the data should be released in the viewDidUnload method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.