Ios-how does the View Controller manage views?
The screen of mobile devices is limited. Everything needs to be displayed on a single interface consisting of a single window, in ios, views are switched (the views are already described in ios-view). When one view replaces another, animations are often used, this task is done by the view Manager. After ios5, the application window has a Root View Controller. When rootViewController is not assigned a value, the warning "Application windows are expected to have a root view controller at the end of application launch" appears. 1. the instantiation method of the View Controller also has several ways to instantiate the View Controller: the first one is code, which is also my favorite one, not because it is simple, it is because the Code makes the program structure clear, simple, and easy to understand. [[ViewController alloc] init]; [[ViewController alloc] initWithNibName: @ "xibName" bundle: nil]; Type 2: Agent class loading xib 1. the xib File requires a ViewController and sets the class in the Identity inspector of the File's Owner of xib as the application proxy class. 2. Add a ViewController outlet variable in the proxy class and connect it. 3. Load the xib file in application: didfinishlaunchingwitexceptions: method. Type 3: Series Diagram 2. how does a view Controller create a view with the loadView method in UIViewController? By default, its view controller does not override this method. I tested it. When I first accessed the view attribute, the loadView method is automatically called and the Code for rewriting this method is as follows:-(void) loadView {self. view = [[UIView alloc] init];} has the same effect as not rewriting, so I guess the loadView method in UIViewController is also implemented in this way (if xib is not used ), thank you for your correction. 3. the following method is declared in the UIViewController of view life cycle: Copy code // load view-(void) loadView; // j: view to be detached-(void) viewWillUnload NS_DEPRECATED_IOS (5_0, 6_0 ); // The view has been uninstalled-(void) viewDidUnload NS_DEPRECATED_IOS (3_0, 6_0); // The view has been loaded-(void) viewDidLoad; // The view will display-(void) viewWillAppear :( BOOL) animated; // The view has been shown-(void) viewDidAppear :( BOOL) animated; // The view will be hidden-(void) viewWillDisappear :( BOOL) animated; // view hidden-(void) viewDidDisappear :( BOOL) animated; // Call layoutSubviews-(void) viewWillLayoutSubviews NS_AVAILABLE_IOS (5_0); // you have called layoutSubviews-(void) viewDidLayoutSubviews NS_AVAILABLE_IOS (5_0 ); by copying code, you can override these methods to manage the lifecycle. 4. one of the main tasks of rotating the View Controller is to know how to rotate the view. There are two different types of rotation: the rotation of the Offset rotation application is to offset the rotation of the device direction, in this way, the application can display the device correctly based on how the user holds the device. The challenge of offset rotation is that the screen is not a square, which means that if the application rotates 90 degrees, the interface is no longer suitable for the screen and must be changed to offset. Force rotation: when a specific view appears on the interface, the application is rotated, or when the application starts east, in order to tell the user to rotate the device to display the view correctly. This is very common, because the interface is specially designed, and the screen is not square, so in fact, only a specific mode (landscape screen or landscape screen) can be displayed ). To support rotation, the view controller needs to rewrite shouldAutorotateToInterfaceOrientation: the input parameter is the direction of the current device, which is one of the following: UIInterfaceOrientationPortrait Home is alive UIInterfaceOrientationLandscapeRight Home on the left returns YES to allow rotation in all directions; otherwise, NO is returned. If this method is not overwritten, YES is returned for uiinterfaceori, if NO is returned in other directions, YES must be returned in some directions. (If the UIViewController attribute is not available, you can set it to the orientation that the view controller can rotate. You can only override this method ). There is a new feature in iOS 5, that is, shouldAutorotateToInterfaceOrientation can be dynamically implemented. Using attemptRotationToDeviceOrientation, this method is used in different scenarios: interface orientation and device orientation, however, you want to re-specify the value of interface orientation to immediately implement the consistency between the two. If only the supported value of interface orientation is changed, attemptRotationToDeviceOrientation is not called, the next time the device orientation changes, the two will be consistent. The key point is whether it can be implemented immediately. For example, assume that the current interface orientation only supports Portrait. If the device orientation becomes Landscape, the interface orientation still displays Portrait; if we want the interface orientation to be the same as the Landscape of device orientation, take iOS 6 as an example. We need to change the return value of supportedInterfaceOrientations to Landscape and then call the attemptRotationToDeviceOrientation method, the system will re-query the supported interface orientation to change the current interface orientation immediately. Rotation events can be directed to the current interface through the interfaceOrientation attribute of the View Controller. The subclass of UIViewController can override the following methods to receive notifications before and after rotation:-(void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration-(void) duration :( UIInterfaceOrientation) fromInterfaceOrientation