UIViewController

时间:2023-03-08 17:54:47

UIViewController 在MVC模式中就是C。关于MVC,可以看

UIViewController 主要具有什么功能呢?

View Management

When you define a new subclass of UIViewController, you must specify the views to be managed by the controller. A typical view hierarchy consists of a view with flexible bounds—a reference to which is available in the view (page 40) property of this class—and one or more subviews that provide the actual content.

当定义一个UIViewController时,必须指定由这个controller管理的views。一个典型的View hierarchy包含一个有fexible bounds的view,和若干个提供实际内容的subview。viewcontroller中View property就是对所管理的view的一个reference。

The size and position of the root view is usually determined by another object that owns the view controller and displays its contents. The view controller determines the positions of any subviews it owns based on the size given to its root view by the owning object.

root view的大小和位置通常由另外一个对象来决定,这个对象own 这个view controller。view controller决定subview的位置。

A view controller is usually owned by a window or another view controller. If a view controller is owned by a window object, it acts as the window’s root view controller. The view controller’s root view is added as a subview of the window and resized to fill the window. If the view controller is owned by another view controller, then the parent view controller determines when and how the
child view controller’s contents are displayed.

一个view controller通常由一个window或者另外一个view controller所拥有。如果它被window拥有,它就是Window的root view controller。view controller的root view被作为window的subview,resize并填满window。如果它被另外一个view controller拥有,父view controller决定子view controller的内容何时和如何被显示出来。

The UIViewController class provides built-in support for loading a view controller’s views whenever they are needed. Specifically, views are automatically loaded when the view property is accessed. 有几种方法可以完成view 和view controller关联的整个过程:

1. storyboard。view 和view controller都被定义在storyboard中,在运行时,viewcontroller根据storyboard中的设置自动实例化和配置它所管理的views。

At runtime, the view controller uses the storyboard to automatically instantiate and configure its views. Often, the view controller itself is automatically created by segues defined in the storyboard. When you define a view controller’s contents using a storyboard, you never directly allocate and initialize the view controller object. Instead, when you need to programmatically instantiate the view controller, you do so by calling the instantiateViewControllerWithIdentifier: method on a UIStoryboard object.

通常,我们不需要直接allocate和初始化在storyboard中的view controller,如果需要,可以使用instantiateViewControllerWithIdentifier:来完成。

2. 使用nib文件。在nib文件中,我们可以定义view controller已经view,但是不能像storyboard那样定义view controller之间的转换关系。可以调用UIViewController的initWithNibName:bundle:来初始化一个view controller。

3. programmatically way。如果不能在storyboard中或者nib文件中定义view,那就在override loadView方法, manually 实例化view hierarchy并赋值给view 属性。

这3种方法的最终结果都是一样的。

Tasks

Creating a View Controller Using Nib Files
– initWithNibName:bundle:
   nibName  property
   nibBundle  property
Using a Storyboard
– shouldPerformSegueWithIdentifier:sender:
– performSegueWithIdentifier:sender:
– prepareForSegue:sender:
   storyboard  property
– canPerformUnwindSegueAction:fromViewController:withSender:
– transitionCoordinator
Managing the View
   view  property
– isViewLoaded
– loadView
– viewDidLoad
   title  property
– viewDidUnload Deprecated in iOS 6.0
– viewWillUnload Deprecated in iOS 6.0
Handling Memory Warnings
– didReceiveMemoryWarning
Responding to View Events
– viewWillAppear:
– viewDidAppear:
– viewWillDisappear:
– viewDidDisappear:
– viewWillLayoutSubviews
– viewDidLayoutSubviews
Testing for Specific Kinds of View Transitions
– isMovingFromParentViewController
– isMovingToParentViewController
– isBeingPresented
– isBeingDismissed
Configuring the View’s Layout Behavior
– updateViewConstraints
   automaticallyAdjustsScrollViewInsets  property
   bottomLayoutGuide  property
   topLayoutGuide  property
   edgesForExtendedLayout  property
   preferredContentSize  property
   extendedLayoutIncludesOpaqueBars  property
– childViewControllerForStatusBarHidden
– childViewControllerForStatusBarStyle
– preferredStatusBarStyle
– prefersStatusBarHidden
   modalPresentationCapturesStatusBarAppearance  property
– preferredStatusBarUpdateAnimation
– setNeedsStatusBarAppearanceUpdate
   wantsFullScreenLayout  property Deprecated in iOS 7.0
Configuring the View Rotation Settings
– shouldAutorotate
– supportedInterfaceOrientations
– preferredInterfaceOrientationForPresentation
   interfaceOrientation  property
+ attemptRotationToDeviceOrientation
– rotatingHeaderView
– rotatingFooterView
– shouldAutorotateToInterfaceOrientation: Deprecated in iOS 6.0
Responding to View Rotation Events
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
– didAnimateFirstHalfOfRotationToInterfaceOrientation: Deprecated in iOS 5.0
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: Deprecated in iOS 5.0
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration: Deprecated in iOS 5.0
Responding to Containment Events
– willMoveToParentViewController:
– didMoveToParentViewController:
Adding Editing Behaviors to Your View Controller
   editing  property
– setEditing:animated:
Managing State Restoration
   restorationIdentifier  property
   restorationClass  property
– encodeRestorableStateWithCoder:
– decodeRestorableStateWithCoder:
– applicationFinishedRestoringState
Presenting Another View Controller’s Content
– presentViewController:animated:completion:
– dismissViewControllerAnimated:completion:
   modalTransitionStyle  property
   modalPresentationStyle  property
   definesPresentationContext  property
   transitioningDelegate  property
   providesPresentationContextTransitionStyle  property
– disablesAutomaticKeyboardDismissal
– dismissModalViewControllerAnimated: Deprecated in iOS 6.0
– presentModalViewController:animated: Deprecated in iOS 6.0
Getting Other Related View Controllers
   presentingViewController  property
   presentedViewController  property
   parentViewController  property
   navigationController  property
   splitViewController  property
   tabBarController  property
   searchDisplayController  property
   modalViewController  property Deprecated in iOS 6.0
Managing Child View Controllers in a Custom Container
   childViewControllers  property
– addChildViewController:
– removeFromParentViewController
– shouldAutomaticallyForwardRotationMethods
– shouldAutomaticallyForwardAppearanceMethods
– transitionFromViewController:toViewController:duration:options:animations:completion:
– beginAppearanceTransition:animated:
– endAppearanceTransition
– viewControllerForUnwindSegueAction:fromViewController:withSender:
– segueForUnwindingToViewController:fromViewController:identifier:
– automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers Deprecated in iOS 6.0
Configuring a Navigation Interface
   navigationItem  property
– editButtonItem
   hidesBottomBarWhenPushed  property
– setToolbarItems:animated:
   toolbarItems  property
Configuring Tab Bar Items
   tabBarItem  property
Configuring Display in a Popover Controller
   modalInPopover  property
   contentSizeForViewInPopover  property Deprecated in iOS 7.0

Reference:

1. https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457-CH1-SW1

2.