UIViewController


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.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM