IOS development-UI (4) UIViewController and iosuiviewcontroller

Source: Internet
Author: User

IOS development-UI (4) UIViewController and iosuiviewcontroller

This knowledge is very important, and it is best to master it all:

Knowledge point:

1. Basic understanding of UIViewController

2. switching between uiviewcontrollers

3. UIViewController Lifecycle

4. How to exchange data between multiple controllers

 

======================================

UIViewController

 

1. MVC design mode

MVC design mode: Model View Controller

 

2. window requires a Root View Controller

RootViewController * rootCtl = [[RootViewController alloc] init];

// Set the Root View Controller

// If the Root View Controller is not replaced, the Root View Controller will not be released.

Self. window. rootViewController = rootCtl;

 

3. The UIViewController object has a built-in view by default.

// Instantiate a View Controller

UIViewController * ctl = [[UIViewController alloc] init];

// Each controller has its own UIView

Ctl. view. alpha = 0.5;

// Set the background color of the View that comes with the Controller (no background color by default)

Ctl. view. backgroundColor = [UIColor whiteColor];

Self. window. backgroundColor = [UIColor orangeColor];

 

4. Relationship between UIViewController and UIView

1) UIViewController is the View Controller, while UIView is the view, that is, UIViewController controls UIView

2) The content (data) to be displayed in UIView must also be managed through UIViewController.

For example, UIViewController is a photo frame, and UIView is a photo.

You can change the photo frame to another one anytime, anywhere, or add a new photo to the photo.

The photo frame cannot be manipulated.

 

======================================

Basic usage of UIViewController

 

1. After the init method is called when the UIViewController is instantiated, the following method is automatically redirected.

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) controller initialization method // init-> initWithNibName: nibNameOrNil bundle:-(instancetype) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil {if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]) {/Note: operations related to the UI are not allowed. Although some operations are not reported, they are neither strict nor prone to some messy problems. // self. view. backgroundColor XXX // allows operations related to data, such as array instantiation and dictionary instantiation.} return self ;}

 

 

2. Functions of viewDidLoad

In general, we will do some additional operations on the interface here, such as adding some subviews to the view,

Load model data from the database or network and assemble it into the subview

 

3. Override the viewDidLoad method.

 

======================================

Switching between UIViewController

 

1. Split the function to different Controller pages

 

2. How to manage switching between multiple uiviewcontrollers

1) switch to the new view controller.

-(Void) presentViewController :( UIViewController *) viewControllerToPresent animated :( BOOL) flag completion :( void (^) (void) completion

// Switch to the new Controller

/*

Parameter 1: controller object to be switched

Parameter 2: whether animation is required

Parameter 3: callback block, generally nil

*/

// Lazy loading mechanism: this object will be instantiated only when used

[self presentViewController:newCtl animated:YES completion:nil];

 

 

2) disable the current view Controller

-(Void) dismissViewControllerAnimated :( BOOL) flag

Completion :( void (^) (void) completion

[self dismissViewControllerAnimated:YES completion:nil];

 

 

3) mode switching:

ModalTransitionStyle

 

UIModalTransitionStyleCoverVertical from bottom to top

UIModalTransitionStyleCrossDissolve gradient

UIModalTransitionStyleFlipHorizontal horizontal flip

UIModalTransitionStylePartialCurl

// Set the animation effect for switching

newCtl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

 

 

======================================

UIViewController Lifecycle

Sequence:

1. alloc creates an object and allocates Space

2. init initializes the object and initializes data.

3. Create LoadView -- self. view

4. Effect of viewDidLoad

5. When viewDidLoad is called

6. viewWillAppear Function

7. viewDidAppear Function

8. viewWillDisappear Function

9. viewDidDisappear Function

10. The dealloc view is destroyed.

// View is about to disappear

Example:

-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    NSLog(@"viewWillDisappear");}

 

 

======================================

How to exchange data between multiple controllers

1. forward data transfer

VA-> VB

One Party to pass the value:

For example, pass the value in RootViewController. m.

NewViewController * newCtl = [NewViewController new];

UILabel * label = (UILabel *) [self. view viewWithTag: 100];

1: newCtl. deliverStr = label. text;

Obtain value:

2: NewViewController. h: @ property (nonatomic, copy) NSString * deliverStr; // accept the passed Parameter

3: NewViewController. m: label. text = self. deliverStr;

 

2. Reverse value transfer

VA <-VB

Value passing party

1: typedef void (^ blockType) (NSString *);

 

@ Interface NewViewController: UIViewController

// Callback block

2: @ property (nonatomic, copy) blockType block;

 

 

Value passing party

NewViewController * newCtl = [NewViewController new];

UILabel * label = (UILabel *) [self. view viewWithTag: 100];

// Set the entity Implementation of the block

3: newCtl. block = ^ (NSString * text ){

Label. text = text;

};

 

3. Pass the value through the AppDelegate object

1) Get the sharedApplication object of the current application:

+ (UIApplication *) sharedApplication transfers the value to UILabel * label = (UILabel *) [self. view viewWithTag: 100]; // assign a value to the transit variable of AppDelgate. // 1. obtain the UIAplication object // 2. get the AppDelgate object // 3. value: UIApplication * app = [UIApplication sharedApplication]; AppDelegate * del = app. delegate; del. middleStr = label. text;

 

 

2) obtain the AppDelegate object of the current application:

@ Property (nonatomic, assign) id <UIApplicationDelegate> delegate;

 

AppDelegate. h

// Intermediate property variable

@property (nonatomic,copy)NSString *middleStr;

 

 

3) Forcibly convert to an AppDelegate object, and pass the value through the setter and getter of the object.

Value side

For example

-(Void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; UIApplication * app = [UIApplication sharedApplication]; AppDelegate * del = app. delegate; // judge if (del. middleStr. length! = 0) {UILabel * label = (UILabel *) [self. view viewWithTag: 100]; label. text = del. middleStr ;}}

 

 

Protocol proxy value passing Method

Create a protocol. h file, that is, the protocol file. The content is as follows:

# Import <Foundation/Foundation. h> @ protocol Myprotocol <NSObject> // the task to be completed by the proxy-(void) showInfo :( NSString *) info; @ end

 

Value Transfer party: complies with the agreement

. H file:

@ Property (nonatomic, weak) id <Myprotocol> delegate; // point to the proxy object

 

. M file:

// Method for executing the proxy object

if ([_delegate respondsToSelector:@selector(showInfo:)]) {        [_delegate showInfo:label.text];    }  

 

 

// Protocol proxy

// Proxy: the object that can complete this task

// The object that you want to complete this task but cannot implement

// Set Proxy: locate the location where both the proxy and the objects to be proxy can be obtained.

 

Obtain value: Party complying with the agreement

In the. h file:

@interface RootViewController : UIViewController<Myprotocol>

 

In the. m file:

-(Void) showInfo :( NSString *) info {// refresh the text content UILabel * Label = (UILabel *) [self. view viewWithTag: 100]; label. text = info ;}

 

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.