IOS stage learning 30th-day notes (UIViewController-Delegate (proxy), iosuiviewcontroller

Source: Internet
Author: User

IOS stage learning 30th-day notes (UIViewController-Delegate (proxy), iosuiviewcontroller

IOS Learning (UI) knowledge point sorting

 

1. Introduction to UIViewController

1) concept: UIViewController is a view controller, which is used to manage and control the page jump class. In iOS, the MVC Architecture is used to facilitate

See View and ViewController. Therefore, UIViewController is a very common and important class in iOS applications.

A class inherits the class of UIViewController. In UIViewController, a very important attribute is View, which corresponds to this Controller.

View, V and C in MVC. You can manually create a View by overwriting the loadView method and set it to the Controller attribute.

2) create a basic ViewController class FirstViewController. Assign the class to the main view controller of window for management. For example:

1 FirstViewController *firstVC = [[FirstViewController alloc] init];2 self.window.rootViewController = firstVC;


3) page Jump Method Instance code

Method 1:

1 // initialize the second page 2 _ secondVC = [[SecondViewController alloc] init]; 3 // jump from the first page to the second page 4 [self presentViewController: _ secondVC animated: YES completion: ^ {5 6}]; 7 // Note: The jump page object here is generally defined as global. Otherwise, an error occurs 8 9 // corresponding jump return method: 10 // return to the previous page. Note: This method is written on the second page. in the m file, 11 [self dismissViewControllerAnimated: YES completion: ^ {12 13}];

 

Method 2:

1 [self. view addSubview: _ secondVC. view]; 2 3 // corresponding jump return method: 4 // return to the previous page note: This method is written on the second page. 5 [self. view removeFromSuperview];

 

4) The default value of backgroundColor during UIViewContrller Initialization is nil rather than clearColor. Therefore, we need to set the View Controller when initializing the View Controller.

A color that is not clearColor, for example:

1  self.view.backgroundColor = [UIColor whiteColor];


II. Introduction to UINavigationController

1) concept: UINavigationController is a class used to control page switching of views.

2) UINavigationController initializes the instance code

1 // initialize a View Controller 2 FirstViewController * firstVC = [[FirstViewController alloc] init]; 3 4 // initialize a navigation controller 5 // The navigation controller has a stack container, during initialization, you must set a base Stack 6 for the rootViewController seat navigation controller // the initialization of the navigation Controller requires a Root View Controller 7 UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController: firstVC]; 8 // set the rootVC of window to the navigation controller instance we initialized 9 self. window. rootViewController = nav;

 

3) pushViewController implements page switching, for example:

1 // initialize SecondViewController2 SecondViewController * secondVC = [[SecondViewController alloc] init]; 3 4 // jump from page 1 to page 25 [self. navigationController pushViewController: secondVC animated: YES];


4) popViewControllerAnimated:

// Return to the previous VC
1 [self. navigationController popViewControllerAnimated: YES]; 2 // Note: push to another vc through the navigation controller, which must be pushed back to the previous page through the navigation controller pop. 3 // The two jump methods must be the corresponding return methods


5) popToRootViewControllerAnimated uses the navigation controller to directly jump from the VC of the navigation controller stack container to the Root View Controller of the navigation controller.

For example:

1 [self.navigationController popToRootViewControllerAnimated:YES];

 

6) viewControllers obtains the View Controller array in the stack container of the navigation controller, for example:

1 NSArray * vcArr = self. navigationController. viewControllers; 2 // obtain the second view controller 3 UIViewController * secondVC = [vcArr objectAtIndex: 1]; 4 5 [self. navigationController popToViewController: secondVC animated: YES];

 

Iii. Introduction to Delegate

1) concept: Delegate (proxy) is a class that declares some protocol methods through the protocol, but does not implement these methods. The process of implementing these methods by specifying other class objects is called

Proxy; you can use a proxy to transfer values between different classes.

2) instance Code implemented by proxy:

1. Create a B View Controller to declare the protocol and Protocol methods in B's. h file, for example:

1 // declare B's protocol. If B's protocol is followed, Information 2 @ protocol ShowMessageDelegate <NSObject> 3 // provide information (protocol method) 4-(void) showMessage :( NSString *) msg; 5 @ end 6 7 @ interface BViewController: UIViewController <UITextFieldDelegate> 8 // create proxy member variable 9 @ property (nonatomic, assign) id <ShowMessageDelegate> delegate; 10 @ end


2. The code in the. m file of View B controller is as follows:

1-(void) viewDidLoad {2 [super viewDidLoad]; 3 self. view. backgroundColor = [UIColor lightGrayColor]; 4 5 UITextField * tf = [[UITextField alloc] init]; 6 tf. frame = CGRectMake (20,150, self. view. frame. size. width-20*2, 40); 7 tf. backgroundColor = [UIColor blackColor]; 8 tf. textColor = [UIColor whiteColor]; 9 tf. delegate = self; 10 [self. view addSubview: tf]; 11} 12 13 // use this method to pass the value entered in the text box in view B controller to the view where the proxy object of the view Controller is located 14-(BOOL) textFieldShouldReturn :( UITextField *) textField15 {16 // self. delegate is the Instance Object of other objects that follow the object protocol in this view 17 // respondsToSelector to determine whether showMessage is implemented: if the method is implemented, yes18 if ([self. delegate respondsToSelector: @ selector (showMessage :)]) {19 [self. delegate showMessage: textField. text]; 20} 21 22 return YES; 23}

 

3. Create A View Controller to act as the proxy of View B to implement the implementation in the Code A View Controller. h file.

1 # import "BViewController. h" 2 @ interface AViewController: UIViewController <ShowMessageDelegate> // comply with the Protocol stated by the B View Controller 3 @ end


4. Implementation code in the View Controller. m file

1-(void) viewDidLoad {2 [super viewDidLoad]; 3 // set the view background color 4 self. view. backgroundColor = [UIColor whiteColor]; 5 6 // page Jump button 7 UIButton * btn = [[UIButton alloc] init]; 8 btn. frame = CGRectMake (0, self. view. frame. size. height-44, self. view. frame. size. width, 44); 9 btn. backgroundColor = [UIColor blackColor]; 10 [btn setTitle: @ "Next" forState: UIControlStateNormal]; 11 [btn addTarget: self action: @ selector (pushToNextVC) forControlEvents: UIControlEventTouchUpInside] 12 [self. view addSubview: btn]; 13 14 // UILabel is used to display the value passed by view B controller. 15 UILabel * lab = [[UILabel alloc] init]; 16 lab. frame = CGRectMake (20,100, self. view. frame. size. width-2*20, 40); 17 lab. backgroundColor = [UIColor blackColor]; 18 lab. textColor = [UIColor whiteColor]; 19 lab. font = [UIFont systemFontOfSize: 20]; 20 lab. tag = 1000; 21 [self. view addSubview: lab]; 22 23} 24 25 # pragma mark-Next page Jump Method 26-(void) pushToNextVC27 {28 BViewController * bVC = [[BViewController alloc] init]; 29 // specify the current view controller object as the proxy object of the B View Controller 30 bVC. delegate = self; 31 [self. navigationController pushViewController: bVC animated: YES]; 32} 33 34 # pragma mark-Protocol method for implementing B View Controller 35-(void) showMessage :( NSString *) msg36 {37 UILabel * lab = (UILabel *) [self. view viewWithTag: 1000]; 38 lab. text = msg; 39}

 

Related Article

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.