你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> UIViewController全部API的學習。

UIViewController全部API的學習。

編輯:關於IOS
/*

UIViewController is a generic controller base class that manages a view.  It has methods that are called

when a view appears or disappears.



Subclasses can override -loadView to create their custom view hierarchy, or specify a nib name to be loaded

automatically.  This class is also a good place for delegate & datasource methods, and other controller

stuff.

*/
@class UIView;

//1. UIViewController是管理一個view的最基本的基類,當一個view要顯示或者消失的時候,UIViewController有相應方法會被調用。子類可以重寫loadView方法去創建他們自定義的view,或者指定一個nib被自動加載實現。這個類也是對於代理,數據源協議方法和一些其他控制器的方法展示實現一個很好的空間地方。

 
@class UINavigationItem, UIBarButtonItem, UITabBarItem;

//2. UINavigationItem是放置在導航欄中的,包含.rightBarButtonItem,.leftBarButtonItem,titleView,而這三個按鈕又可以歸為UIBarButtonItem類,並通過它創建。UITabBarItem是分欄控制器底部切換item按鈕。

 
@class UISearchDisplayController

//3. UISearchDisplayController是專門為UITableView搜索封裝的一個類。

 
@class UIPopoverController;

//4.UIPopverController是IPad特有的類,不能在iphone上使用,它負責控制Popover視圖。Popover是一種臨時視圖,它以“漂浮”的形式出現在視圖裡面。

 
@class UIStoryboard, UIStoryboardSegue;

//5. UIStoryboard是iOS5以後新增的內容,是以故事版的形式來展示界面之間的邏輯業務關系,前端開發目前有三種方式(純代碼,代碼+xib,UIStoryboard),UIStoryboard為其中一種。而UIStoryboardSegue—————————————————————
@class UIScrollView

//6. UIScrollView在UIKit中是UIScrollView類,是容器類型的視圖。它有三個子類—UITextView,UITableview和UICollectionView,它們在內容超出屏幕時提供水平或垂直滾動條。
@protocol UIViewControllerTransitionCoordinator;

//7.這就涉及到自定義viewController容器轉場,iOS7引入的新特性自定義viewController轉場。-----------

 
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {

UIModalTransitionStyleCoverVertical =
0,

UIModalTransitionStyleFlipHorizontal,

UIModalTransitionStyleCrossDissolve,

UIModalTransitionStylePartialCurl
NS_ENUM_AVAILABLE_IOS(3_2),

};

//8.UIModalTransitionStyle是彈出模態ViewController時的四種動畫風格,UIModalTransitionStyleCoverVertical是從底部滑入,UIModalTransitionStyleFlipHorizontal是水平翻轉,UIModalTransitionStyleCrossDissolve是交叉溶解,UIModalTransitionStylePartialCurl是翻頁效果。

 
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {

UIModalPresentationFullScreen =
0,

UIModalPresentationPageSheet
NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationFormSheet
NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationCurrentContext
NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationCustom
NS_ENUM_AVAILABLE_IOS(7_0),

UIModalPresentationOverFullScreen
NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationOverCurrentContext
NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationPopover
NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationNone
NS_ENUM_AVAILABLE_IOS(7_0) = -1,

};

//9,UIModalPresentationStyle是彈出模態ViewController時彈出風格,UIModalPresentationFullScreen是彈出VC時,VC充滿全屏,UIModalPresentationPageSheet是如果設備橫屏,VC的顯示方式則從橫屏下方開始,UIModalPresentationFormSheet是VC顯示都是從底部,寬度和屏幕寬度一樣。UIModalPresentationCurrentContext是VC的彈出方式和VC父VC的彈出方式相同。.....UIModalPresentationNone測試時會崩潰,出錯'The
specified modal presentation style doesn't have a corresponding presentation controller.'-------------------------------

 
@protocol UIContentContainer <NSObject>

//10.iOS8之後,加入了新的一組協議,UIViewController對這組協議提供了默認的實現,我們自定義ViewConttroller的時候可以重寫這些方法來調整視圖布局。



 
@property (nonatomic, readonly) CGSize preferredContentSize
NS_AVAILABLE_IOS(8_0);

//11.我們可以使用preferredContentSize來設置我們期望的childViewController的界面的大小。





- (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container
NS_AVAILABLE_IOS(8_0);

//12.這個方法告訴當前容器viewController,我們可以在這個方法裡根據新的size對界面進行調整。
/*

Intended as a bridge for a view controller that does not use auto layout presenting a child that does use auto layout.



If the child's view is using auto layout and the -systemLayoutSizeFittingSize: of the view

changes, -systemLayoutFittingSizeDidChangeForChildContentContainer: will be sent to the view controller's parent.

*/



- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container
NS_AVAILABLE_IOS(8_0);

//13.重寫方法來調整視圖布局,當在這些情況下該方法會被調用,當前viewController沒有使用auto layout布局,childrenViewController的view使用了autoLayout布局childrenViewController View-systemLayoutSizeFittingSize:方法返回的值的改變(view由於內容的改變,size也出現了變化)
/*

When the content container forwards viewWillTransitionToSize:withTransitionCoordinator: to its children, it will call this method to determine what size to send them.



If the returned size is the same as the child container's current size, viewWillTransitionToSize:withTransitionCoordinator: will not be called.

*/



- (CGSize)sizeForChildContentContainer:(id <UIContentContainer>)container withParentContainerSize:(CGSize)parentSize
NS_AVAILABLE_IOS(8_0);

//14.同上,依舊因為iOS8上size class概念的提出,UIViewConteroller支持了UIConntentContainer這樣一組新的協議,重寫這些方法可以調整視圖布局,一個容器viewController可以使用這個方法設置childViewController的size,當容器viewControllerViewWillTransitionTosize:withTransitionCoordinator:被調用時(我們重寫這個方法時要調用super),sizeForChildContentContainer方法將被調用。然後我們可以把需要設置desire發送給childViewController。當我們設置的這個size和當前childViewController的size一樣,那麼childViewController的viewWillTransitionToSize方法將不會被調用。sizeForChildContentContainer默認的實現是返回parentSize
/*

This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized).

If you override this method, you should either call super to propagate the change to children or manually forward the change to children.

*/

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
NS_AVAILABLE_IOS(8_0);

//15.viewController的view的size被他的parent Controller改變時,會觸發這個方法(比如rootViewController在它的window旋轉的時候)我們重寫這個方法時,確保要調用super來保證size改變的這條消息能夠正常傳遞給它的views活著childViewControllers。
/*

This method is called when the view controller's trait collection is changed by its parent.



If you override this method, you should either call super to propagate the change to children or manually forward the change to children.

*/





- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id
<UIViewControllerTransitionCoordinator>)coordinator
NS_AVAILABLE_IOS(8_0);

//17.當viewController的traitCollection的值將要改變時會調用這個方法,這個方法是在UITraitEnvironment協議方法traitCollectionDidChange:之前被調用。我們在重寫這個方法時候也要確保要調用super來保證消息的傳遞。比如,我們可以像這樣在traintCollection值改變時,對試圖做相應的動畫進行調整。
@end



// Sometimes view controllers that are using showViewController:sender and showDetailViewController:sender: will need to know when the split view controller environment above it has changed. This notification will be posted
when that happens (for example, when a split view controller is collapsing or expanding). The NSNotification's object will be the view controller that caused the change.
UIKIT_EXTERN NSString *const UIViewControllerShowDetailTargetDidChangeNotification
NS_AVAILABLE_IOS(8_0);

//18.

 
NS_CLASS_AVAILABLE_IOS(2_0)
@interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer> {

//19.UIViewController繼承自UIResponder類,並實現NSCoding(實現歸檔操作),UIAppearanceContainer(允許開發者對UI控件通過定制都可以呈現各種外觀),UITraitEnvironment(SizeClasses適配理念,通過內部的traitCollection屬性,可以拿到對應的UITraitCollection對象,從而得知當前的SizeClass,進一步確定界面的布局),UIContentContainer協議(調整視圖布局)

 
@package

//20.實例變量范圍類型@package,對於framework內部相當於@protect,對於framework外部相當於@private。

UIView           *_view;

//21.view成員變量

UITabBarItem     *_tabBarItem;

//22.分欄控制器的item按鈕成員變量

UINavigationItem *_navigationItem;

//23.導航欄item成員變量

NSArray          *_toolbarItems;

//24.數組用來保存工具條按鈕item的成員變量

NSString         *_title;

//25.標題title成員變量

NSString         *_nibName;

//26.nib名稱成員變量

NSBundle         *_nibBundle;

//27.bundle成員變量

UIViewController *_parentViewController;
// Nonretained

//28.取父viewConreoller成員變量

UIViewController *_childModalViewController;

//29.取子模態ViewController成員變量

UIViewController *_parentModalViewController;
// Nonretained

//30.取父模態ViewController成員變量

UIViewController *_previousRootViewController;
// Nonretained

//31.取上一個根previous的成員變量

UIView           *_modalTransitionView;

//32.模態過渡view的成員變量

UIResponder
*_modalPreservedFirstResponder;

//33.第一個保存Responder成員變量
id               _dimmingView;

//34.變暗的view-----------
id               _dropShadowView;

//35.刪除陰影view成員變量--------------
id                _currentAction;

//36.當前動作方法

UIStoryboard     *_storyboard;

//37.storyboard成員變量

NSArray          *_storyboardSegueTemplates;

//38.存放storyboardSegue的數組

NSDictionary     *_externalObjectsTableForViewLoading;

//39.--------------------

NSArray          *_topLevelObjectsToKeepAliveFromStoryboard;

//40.--------------------

UIView           *_savedHeaderSuperview;

//41.表頭的父view

UIView           *_savedFooterSuperview;

//42.表尾的父view

UIBarButtonItem  *_editButtonItem;

//43.可編輯的BarButtonItem

UISearchDisplayController *_searchDisplayController;

//44.用於搜索的viewController

UIModalTransitionStyle _modalTransitionStyle;

//45.模態視圖過渡動畫樣式

UIModalPresentationStyle _modalPresentationStyle;

//46.模態視圖呈現樣式

UIInterfaceOrientation _lastKnownInterfaceOrientation;

//47.當前程序界面的旋轉方向

UIPopoverController*    _popoverController;

//48.IPad專用的一種臨時視圖,以漂浮的形式出現在屏幕表面。

UIView *_containerViewInSheet;

//49.---------------

CGSize _contentSizeForViewInPopover;

//50.在IPad臨時視圖中展示容納的view大小

CGSize _formSheetSize;

//51.---------

UIScrollView *_recordedContentScrollView;

//52.-----------
void (^_afterAppearance)(void);

//53.---------------

NSInteger _explicitAppearanceTransitionLevel;

//54.------------

NSArray *_keyCommands;

//55.------------

NSMapTable *_overrideTraitCollections;

//56.--------了解過還是不太理解



 
/*

The designated initializer. If you subclass UIViewController, you must call the super implementation of this

method, even if you aren't using a NIB.  (As a convenience, the default init method will do this for you,

and specify nil for both of this methods arguments.) In the specified NIB, the File's Owner proxy should

have its class set to your view controller subclass, with the view outlet connected to the main view. If you

invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose

name is the same as your view controller's class. If no such NIB in fact exists then you must either call

-setView: before -view is invoked, or override the -loadView method to set up your views programatically.

*/

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

//100.這個方法是加載對應nibName的viewController





@property(nonatomic,retain) UIView *view;
// The getter first invokes [self loadView] if the view hasn't been set yet. Subclasses must call super if they override the setter or getter.

//101.view屬性,如果當你第一次調用[self loadView]方法而view還沒有被設置,子類必須要調用super父類方法重寫覆蓋setter和getter



- (void)loadView;
// This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.

//102.在loadView方法內部可以創建自定義的view,有一前提是沒有使用xib。還有這個方法絕對不能直接主動去調用。



- (void)viewWillUnload
NS_DEPRECATED_IOS(5_0,6_0);

//103.view將要銷毀的時候會調用這個方法(不過現在已棄用)



- (void)viewDidUnload
NS_DEPRECATED_IOS(3_0,6_0);
// Called after the view controller's view is released and set to nil. For example, a memory warning which causes the view to be purged. Not invoked as a result of -dealloc.

//104.view已經被銷毀的時候會調用這個方法(現在也已棄用)



- (void)viewDidLoad;
// Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.

//105.這個方法在view加載完畢的時候調用,在此方法內部對view做一些渲染的事情,做一些初始化的操作,並且執行順序是在loadView方法調用之後調用,



- (BOOL)isViewLoaded
NS_AVAILABLE_IOS(3_0);

//106.判斷view是否被加載完畢



@property(nonatomic, readonly, copy) NSString *nibName;
// The name of the nib to be loaded to instantiate the view.

//107.nibName作為屬性,加載對應的nib。

 
@property(nonatomic, readonly, retain) NSBundle *nibBundle;
// The bundle from which to load the nib.

//108.bundle是一個資源目錄,包含nib文件,nibBunlde作為屬性

 
@property(nonatomic, readonly, retain) UIStoryboard *storyboard
NS_AVAILABLE_IOS(5_0);

//109.storyboard屬性,iOS5出現



- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
NS_AVAILABLE_IOS(5_0);

//110.使用stortboard時利用segue進行頁面跳轉的方法



- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
NS_AVAILABLE_IOS(6_0);
// Invoked immediately prior to initiating a segue. Return NO to prevent the segue from firing. The default implementation returns YES. This method is not invoked when -performSegueWithIdentifier:sender: is used.

//111.這份方法是決定是否執行segeue的關鍵。可在內部做一些時間判斷,返回yes,將執行performSegueWithIdentifier:方法,返回no不執行performSegueWithIdentifier:,默認返回yes,可以執行



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
NS_AVAILABLE_IOS(5_0);

//112.當用storyboard並且觸發segue時,系統會自動調用這個方法,可以重寫這個方法,在內部做一些類與類傳遞數據的操作



// View controllers will receive this message during segue unwinding. The default implementation returns the result of -respondsToSelector: - controllers can override this to perform any ancillary checks, if necessary.

- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController
withSender:(id)sender
NS_AVAILABLE_IOS(6_0);

//113.我們一般在子控制器中通過實現canperformUnwindSegueAction:fromViewController:withSender:來決定要不要執行相應的Unwind
Segue。



// Custom containers should override this method and search their children for an action handler (using -canPerformUnwindSegueAction:fromViewController:sender:). If a handler is found, the controller should return it.
Otherwise, the result of invoking super's implementation should be returned.

- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
NS_AVAILABLE_IOS(6_0);

//114.這個方法用來決定哪個viewController來處理Unwind Segue action



// Custom container view controllers should override this method and return segue instances that will perform the navigation portion of segue unwinding.

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString
*)identifier NS_AVAILABLE_IOS(6_0);

//115.這個方法用來返回自定義的Unwind Segue實例



- (void)viewWillAppear:(BOOL)animated;
// Called when the view is about to made visible. Default does nothing

//116.這個方法是當view將要顯示的時候被調用



- (void)viewDidAppear:(BOOL)animated;
// Called when the view has been fully transitioned onto the screen. Default does nothing

//117.這個方法是當view已經顯示的時候被調用



- (void)viewWillDisappear:(BOOL)animated;
// Called when the view is dismissed, covered or otherwise hidden. Default does nothing

//118.這個方法是當view將要消失的時候調用



- (void)viewDidDisappear:(BOOL)animated;
// Called after the view was dismissed, covered or otherwise hidden. Default does nothing

//119.這個方法是當view已經消失的時候調用



// Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

- (void)viewWillLayoutSubviews
NS_AVAILABLE_IOS(5_0);

//120.在重新布局子view前的時候會調用



// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

- (void)viewDidLayoutSubviews
NS_AVAILABLE_IOS(5_0);

//120.在重新布局子view後的時候會調用



@property(nonatomic,copy) NSString *title;
// Localized title for use by a parent controller.

//121.標題title屬性



- (void)didReceiveMemoryWarning;
// Called when the parent application receives a memory warning. On iOS 6.0 it will no longer clear the view by default.

//122.當應用程序收到內存警告的時候會調用這個方法 ,做相應的解決內存警告的操作

 
/*

If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar

controller,) this is the containing view controller.  Note that as of 5.0 this no longer will return the

presenting view controller.

*/
@property(nonatomic,readonly) UIViewController *parentViewController;

//123.取父viewController的屬性



// This property has been replaced by presentedViewController.
@property(nonatomic,readonly) UIViewController *modalViewController
NS_DEPRECATED_IOS(2_0,
6_0);

//124.得到模態視圖控制器



// The view controller that was presented by this view controller or its nearest ancestor.
@property(nonatomic,readonly) UIViewController *presentedViewController
NS_AVAILABLE_IOS(5_0);

//125.------------------------



// The view controller that presented this view controller (or its farthest ancestor.)
@property(nonatomic,readonly) UIViewController *presentingViewController
NS_AVAILABLE_IOS(5_0);

//126.正在展示的控制器

 
/*

Determines which parent view controller's view should be presented over for presentations of type

UIModalPresentationCurrentContext.  If no ancestor view controller has this flag set, then the presenter

will be the root view controller.

*/
@property(nonatomic,assign)
BOOL definesPresentationContext NS_AVAILABLE_IOS(5_0);

//127.
決定哪個父viewController的view應當呈現在當前,如果沒有父viewController,呈現的將是根視圖控制器。





// A controller that defines the presentation context can also specify the modal transition style if this property is true.
@property(nonatomic,assign)
BOOL providesPresentationContextTransitionStyle
NS_AVAILABLE_IOS(5_0);

//128.如果這個屬性是true的話,可以定義模態過渡轉場的樣式



 
/*

These four methods can be used in a view controller's appearance callbacks to determine if it is being

presented, dismissed, or added or removed as a child view controller. For example, a view controller can

check if it is disappearing because it was dismissed or popped by asking itself in its viewWillDisappear:

method by checking the expression ([self isBeingDismissed] || [self isMovingFromParentViewController]).

*/



- (BOOL)isBeingPresented
NS_AVAILABLE_IOS(5_0);

//129.判斷viewController是否正在展示



- (BOOL)isBeingDismissed
NS_AVAILABLE_IOS(5_0);

//130.判斷viewController是否正在消失



- (BOOL)isMovingToParentViewController
NS_AVAILABLE_IOS(5_0);

//131.判斷是否正在向父viewController移動



- (BOOL)isMovingFromParentViewController
NS_AVAILABLE_IOS(5_0);

//132.判斷是否正由父viewController移動而來

 
/*

The next two methods are replacements for presentModalViewController:animated and

dismissModalViewControllerAnimated: The completion handler, if provided, will be invoked after the presented

controllers viewDidAppear: callback is invoked.

*/

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void
(^)(void))completion
NS_AVAILABLE_IOS(5_0);

//133.這個方法可以實現從當前viewController模態跳轉到另一個viewController,completion可在viewDidAppear後回調



// The completion handler, if provided, will be invoked after the dismissed controller's viewDidDisappear: callback is invoked.

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void
(^)(void))completion
NS_AVAILABLE_IOS(5_0);

//134.這個方法可以實現退出當前viewController由模態方式展現出來的,completion可在viewDidDisappear後回調



// Display another view controller as a modal child. Uses a vertical sheet transition if animated.This method has been replaced by presentViewController:animated:completion:

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
NS_DEPRECATED_IOS(2_0,
6_0);

//135.呈現模態視圖



// Dismiss the current modal child. Uses a vertical sheet transition if animated. This method has been replaced by dismissViewControllerAnimated:completion:

- (void)dismissModalViewControllerAnimated:(BOOL)animated
NS_DEPRECATED_IOS(2_0,
6_0);

//136.退出模態視圖

 
/*

Defines the transition style that will be used for this view controller when it is presented modally. Set

this property on the view controller to be presented, not the presenter.  Defaults to

UIModalTransitionStyleCoverVertical.

*/
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle
NS_AVAILABLE_IOS(3_0);

//137.當一個viewController以模態方式呈現的時候,通過modalTransitionStyle可設置過渡動畫樣式

 
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle
NS_AVAILABLE_IOS(3_2);

//137.當一個viewController以模態方式呈現的時候,通過modalPresentationStyle可設置呈現的樣式



// This controls whether this view controller takes over control of the status bar's appearance when presented non-full screen on another view controller. Defaults to NO.
@property(nonatomic,assign)
BOOL modalPresentationCapturesStatusBarAppearance
NS_AVAILABLE_IOS(7_0);

//138.這個屬性是控制是否顯示狀態欄





// Presentation modes may keep the keyboard visible when not required. Default implementation affects UIModalPresentationFormSheet visibility.

- (BOOL)disablesAutomaticKeyboardDismissal
NS_AVAILABLE_IOS(4_3);

//139.這個API是於關閉鍵盤相關的,通常情況下單純的使用resignFirstResponder即可關閉鍵盤,但是在UIModalPresentationFormSheet下的視圖是無法用resignFirstResponder這個API,是因為在進入到此模式後,系統將disablesAutomaticKeyboardDismissal的方法返回值設置成了YES,所以解決方法很簡單,重新實現disablesAutomaticKeyboardDismissal,如果是直接使用viewController則可以在實現文件中重新實現這個API,將返回改為NO後,即可正常使用resignFirstResponder方法隱藏鍵盤,但很多時候我們是把viewController放在UINavigationController中的,這種情況下直接在viewController這裡面實現disablesAutomaticKeyboardDismissal依然失效,而應發iUINavigationController中實現這個API。

 
@property(nonatomic,assign)
BOOL wantsFullScreenLayout NS_DEPRECATED_IOS(3_0,
7_0); // Deprecated in 7_0, Replaced by the following:

//140.wantsFullScreenLayout=true,狀態欄的高度就不會被算在視圖裡,也就是說有沒有狀態欄y坐標始終都是從0算起(不過此屬性在iOS7已被棄用,下面屬性替代此功能)

 
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout
NS_AVAILABLE_IOS(7_0);
// Defaults to UIRectEdgeAll

//141.iOS7的新屬性,默認值為UIRectEdgeAll,在navigationController中view的默認布局從navigationBar開始,self.edgesForExtendedLayout=UIRectEdgeNone,則布局控件減去navigationBar----------------



@property(nonatomic,assign)
BOOL extendedLayoutIncludesOpaqueBars
NS_AVAILABLE_IOS(7_0);
// Defaults to NO, but bars are translucent(透明的,半透明的)by default on 7_0.

//142.在iOS7中UIViewController的wantsFullScreenLayout已經被捨棄,不在提供控件自動展示到導航欄下面這個屬性,所有的UIViewController創建後默認就是full
Screen的,因此如果帶導航欄的應用界面中的部分控件會被導航欄覆蓋掉,要解決這個情況可以使用iOS7中UIVIewController新增的屬性extendedLayoutIncludesOpaqueBars和edgesForExtendedLayout來解決。其中這個屬性指定了當bar使用了不透明圖片時,視圖是否眼神至bar所在區域,默認值是NO,而edgesForExtendedLayout則是表示食欲是夠覆蓋到四周的區域,默認是UIRectEdgeAll,即上下左右四個方向都會覆蓋,那麼為讓頂部不進行眼神到導航欄覆蓋的區域,我們可以把頂部區域眼神去掉,實現代碼如下,self.extendedLayoutIncludesOpaqueBars
= NO;

self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;

當你在做的時候發現viewDidLoad的時候view的高度還是全屏,viewWillAppear已經去掉navigationbar了,所以frame的設置放在了viewWillAppear中比較准確。

 
@property(nonatomic,assign)
BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0);
// Defaults to YES

//143.當在一個viewController中有多個tableView之類的時候,要把這個屬性設置為NO,這樣多個tableview的位置就會是按照自己設定的來,而不會出現位置意外不對的情況



/* The preferredContentSize is used for any container laying out a child view controller.

*/
@property (nonatomic) CGSize preferredContentSize
NS_AVAILABLE_IOS(7_0);

//144.preferredContentSize來設置我們期望的childViewController的界面的大小



// These methods control the attributes of the status bar when this view controller is shown. They can be overridden in view controller subclasses to return the desired status bar attributes.

- (UIStatusBarStyle)preferredStatusBarStyle
NS_AVAILABLE_IOS(7_0);
// Defaults to UIStatusBarStyleDefault

//145.設置shown的ViewController的狀態欄樣式



- (BOOL)prefersStatusBarHidden
NS_AVAILABLE_IOS(7_0);
// Defaults to NO

//146.設置狀態欄是否隱藏顯示



// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
NS_AVAILABLE_IOS(7_0);
// Defaults to UIStatusBarAnimationFade

//147.重寫此方法設置狀態欄更新時候的動畫



// This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the
animation block.

- (void)setNeedsStatusBarAppearanceUpdate
NS_AVAILABLE_IOS(7_0);

//148.如果需要對狀態欄進行刷新,調用此方法即可



/* This method returns either itself or the nearest ancestor that responds to the action. View controllers can return NO from canPerformAction:withSender: to opt out of being a target for a given action. */

- (UIViewController *)targetViewControllerForAction:(SEL)action sender:(id)sender
NS_AVAILABLE_IOS(8_0);

//149.



/* This method will show a view controller appropriately for the current size-class environment. It's implementation calls

`[self targetViewControllerForAction:sender:]` first and redirects accordingly if the return value is not `self`, otherwise it will present the vc. */

- (void)showViewController:(UIViewController *)vc sender:(id)sender
NS_AVAILABLE_IOS(8_0);

//150.-----------------



/* This method will show a view controller within the semantic "detail" UI associated with the current size-class environment. It's implementation calls  `[self targetViewControllerForAction:sender:]` first and redirects
accordingly if the return value is not `self`, otherwise it will present the vc.  */

- (void)showDetailViewController:(UIViewController *)vc sender:(id)sender
NS_AVAILABLE_IOS(8_0);

//151.--------------------------



 
@end



// To make it more convenient for applications to adopt rotation, a view controller may implement the below methods. Your UIWindow's frame should use [UIScreen mainScreen].bounds as its frame.
@interface UIViewController (UIViewControllerRotation)

//152.為UIViewController添加分類方法
為了更好的適應應用的屏幕旋轉



// call this method when your return value from shouldAutorotateToInterfaceOrientation: changes

// if the current interface orientation does not match the current device orientation, a rotation may occur provided all relevant view controllers now return YES from shouldAutorotateToInterfaceOrientation:

+ (void)attemptRotationToDeviceOrientation
NS_AVAILABLE_IOS(5_0);

//153.這個方法這樣使用:當應用的device orientation轉換方向,又希望interface orientation也跟著轉換方向時,不僅僅是將supportedInterfaceOrientations的返回值設置為目標方向,仍需要調用attemptRotationToDeviceOrientation這個方法,這樣才能讓interface
orientation和device orientation的方向立即保持一致。



// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
NS_DEPRECATED_IOS(2_0,
6_0);

//154.iOS6之前我們可以通過這個方法來單獨控制某個UIVewController的旋轉方向



// New Autorotation support.

- (BOOL)shouldAutorotate
NS_AVAILABLE_IOS(6_0);

//155.決定是否之處多方向旋轉屏,返回NO不支持,只會支持默認的UIInterfaceOrientaionMaskPortrait方向



- (NSUInteger)supportedInterfaceOrientations
NS_AVAILABLE_IOS(6_0);

//156.此方法的返回值決定interface orienation的旋轉方向



// Returns interface orientation masks.

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
NS_AVAILABLE_IOS(6_0);

//157.這個方法的作用是設置viewController在present時首選的默認方向,返回一個interfaceOrientation



// The rotating header and footer views will slide out during the rotation and back in once it has completed.

- (UIView *)rotatingHeaderView NS_DEPRECATED_IOS(2_0,8_0,
"Header views are animated along with the rest of the view hierarchy");
// Must be in the view hierarchy. Default returns nil.

//158.查詢當前的導航視圖



- (UIView *)rotatingFooterView NS_DEPRECATED_IOS(2_0,8_0,
"Footer views are animated along with the rest of the view hierarchy");
// Must be in the view hierarchy. Default returns nil.

//159.查詢當前的標簽視圖

 
@property(nonatomic,readonly) UIInterfaceOrientation interfaceOrientation
NS_DEPRECATED_IOS(2_0,8_0);

//160.interfaceOrientation是應用界面的旋轉方向



// Notifies when rotation begins, reaches halfway point and ends.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
NS_DEPRECATED_IOS(2_0,8_0,
"Implement viewWillTransitionToSize:withTransitionCoordinator: instead");

//161.應用界面將要旋轉的時候前會調用,一般用來禁用某些控件或者停止某些正在運行的動作,比如播放視頻等等



- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
NS_DEPRECATED_IOS(2_0,8_0);

//162.這個發生在旋轉屏幕之後,一般用來啟用某些之前被禁用的控件或者是之前已經正在運行的動作,如播放視頻等等



- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
NS_DEPRECATED_IOS(3_0,8_0,
"Implement viewWillTransitionToSize:withTransitionCoordinator: instead");

//163.這個方法發生在旋轉的過程中,一般用來定制翻轉後各個控件的位置大小等



- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
NS_DEPRECATED_IOS(2_0,
5_0);

//164.這個方法和163那個方法作用一樣,就是作用范圍時間不一樣,作為翻轉的前半段



- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
NS_DEPRECATED_IOS(2_0,
5_0); // The rotating header and footer views are offscreen.

//165.這個方法是在前半段旋轉之後發生,做一些相應操作



- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
NS_DEPRECATED_IOS(2_0,
5_0); // A this point, our view orientation is set to the new orientation.

//166.這個方法是也是發生在旋轉過程中,和163一樣作樣,發生作用范圍在後半段

 
@end



// Many view controllers have a view that may be in an editing state or not- for example, a UITableView.  These view

// controllers can track the editing state, and generate an Edit|Done button to be used in a navigation bar.
@interface UIViewController (UIViewControllerEditing)

//167.為UIViewController添加分類方法
實現可編輯的功能

 
@property(nonatomic,getter=isEditing)
BOOL editing;

//168.bool值
判斷是否可編輯



- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
// Updates the appearance of the Edit|Done button item as necessary. Clients who override it must call super first.

//169.更新Edit|Done按鈕item的外觀



- (UIBarButtonItem *)editButtonItem; // Return an Edit|Done button that can be used as a navigation item's custom view. Default action toggles the editing state with animation.

//170.實現一個自定義的view,作為導航欄的Edit|Done按鈕。

 
@end

 
@interface UIViewController (UISearchDisplayControllerSupport)

//171.實現UISearchDisplayControllerSupport相關的分類方法

 
@property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController
NS_DEPRECATED_IOS(3_0,8_0);

//172.UIViewController自帶searchDisplayController屬性

 
@end



 
/*

This exception is raised when a child view controller's view is added into the view hierarchy and the first

superview of the child view controller's view that has a view controller is NOT the child view controller's

parent.

*/
UIKIT_EXTERN NSString *const UIViewControllerHierarchyInconsistencyException
NS_AVAILABLE_IOS(5_0);

//173.異常。因控制器的層次與視圖的層次不一致會引發該異常,當視圖控制器的視圖添加到視圖層中,系統會遍歷視圖層來找到第一個擁有視圖控制器的父視圖。找到的該父視圖控制器也必須是剛添加的視圖的所對應視圖控制器的父控制器。否則會拋出異常。當視圖控制器通過addChildViewController方法被添加進來後,也會進行之前的一致性檢測。不存在俯視圖控制器的視圖控制器,也可以將其視圖添加到視圖層中。但通常極少數情況下才會用到這種方式。

 
/*

The methods in the UIContainerViewControllerProtectedMethods and the UIContainerViewControllerCallbacks

categories typically should only be called by subclasses which are implementing new container view

controllers. They may be overridden but must call super.

*/
@interface UIViewController (UIContainerViewControllerProtectedMethods)

//174.添加新類別ContainerViewController,那什麼是ContainerViewController,意思就是一個viewController顯示的某部分內容屬於另一個viewController,那麼這個viewController就是一個container,比如UIKit的UINavigationController,UITabBarController



// An array of children view controllers. This array does not include any presented view controllers.
@property(nonatomic,readonly) NSArray *childViewControllers
NS_AVAILABLE_IOS(5_0);

//175.數組用來存放子控制器

 
/*

If the child controller has a different parent controller, it will first be removed from its current parent

by calling removeFromParentViewController. If this method is overridden then the super implementation must

be called.

*/

- (void)addChildViewController:(UIViewController *)childController
NS_AVAILABLE_IOS(5_0);

//176.添加子視圖控制器

 
/*

Removes the the receiver from its parent's children controllers array. If this method is overridden then

the super implementation must be called.

*/

- (void) removeFromParentViewController
NS_AVAILABLE_IOS(5_0);

//177.從父控制器中移除子控制器



 
/*

This method can be used to transition between sibling child view controllers. The receiver of this method is

their common parent view controller. (Use [UIViewController addChildViewController:] to create the

parent/child relationship.) This method will add the toViewController's view to the superview of the

fromViewController's view and the fromViewController's view will be removed from its superview after the

transition completes. It is important to allow this method to add and remove the views. The arguments to

this method are the same as those defined by UIView's block animation API. This method will fail with an

NSInvalidArgumentException if the parent view controllers are not the same as the receiver, or if the

receiver explicitly forwards its appearance and rotation callbacks to its children. Finally, the receiver

should not be a subclass of an iOS container view controller. Note also that it is possible to use the

UIView APIs directly. If they are used it is important to ensure that the toViewController's view is added

to the visible view hierarchy while the fromViewController's view is removed.

*/

- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
NS_AVAILABLE_IOS(5_0);

//178.不同的view放在不同的viewController中,用到才去創建,那麼這個方法是實現兩個自控制器的切換,參數fromViewController是當前顯示在父控制器中的子視圖控制器,toViewController是將要顯示的子視圖控制器,duration完成過渡時間,options指定的過渡效果,animations動畫,completion過渡完成後,回調的block塊操作



// If a custom container controller manually forwards its appearance callbacks, then rather than calling

// viewWillAppear:, viewDidAppear: viewWillDisappear:, or viewDidDisappear: on the children these methods

// should be used instead. This will ensure that descendent child controllers appearance methods will be

// invoked. It also enables more complex custom transitions to be implemented since the appearance callbacks are

// now tied to the final matching invocation of endAppearanceTransition.

- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated
__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

//179.beginAppearanceTransition觸發相應的viewVWillAppear????????????



- (void)endAppearanceTransition
__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

//180.endAppearanceTransition觸發相應的viewWillDisappear





// Override to return a child view controller or nil. If non-nil, that view controller's status bar appearance attributes will be used. If nil, self is used. Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes
should be called.

- (UIViewController *)childViewControllerForStatusBarStyle
NS_AVAILABLE_IOS(7_0);

//181.設置子控制器的狀態欄樣式



- (UIViewController *)childViewControllerForStatusBarHidden
NS_AVAILABLE_IOS(7_0);

//182.實現此方法來指定要控制的狀態欄隱藏和取消隱藏狀態



// Call to modify the trait collection for child view controllers.

- (void)setOverrideTraitCollection:(UITraitCollection *)collection forChildViewController:(UIViewController *)childViewController
NS_AVAILABLE_IOS(8_0);

//183.通過此方法可以為子控制器重新設置traintCollection的值,一般情況下traitCollection值從父controller傳到子controller是不做修改的,而我們自己實現一個容器controller的時候,我們可以使用這個方法進行調整



- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
NS_AVAILABLE_IOS(8_0);

//184.這個方法可以幫助我們獲得子控制器的trainCollection值。
@end

 
@interface UIViewController (UIContainerViewControllerCallbacks)

//185.關於ContainerViewController一些回調方法

 
/*

This method is consulted to determine if a view controller manually forwards its containment callbacks to

any children view controllers. Subclasses of UIViewController that implement containment logic may override

this method. The default implementation returns YES. If it is overridden and returns NO, then the subclass is

responsible for forwarding the following methods as appropriate - viewWillAppear: viewDidAppear: viewWillDisappear:

viewDidDisappear: willRotateToInterfaceOrientation:duration:

willAnimateRotationToInterfaceOrientation:duration: didRotateFromInterfaceOrientation:

*/



- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
NS_DEPRECATED_IOS(5_0,6_0);

//186.在iOS5中覆蓋此方法可以關閉appearance callbacks的自動傳遞特性



- (BOOL)shouldAutomaticallyForwardRotationMethods
NS_DEPRECATED_IOS(6_0,8_0,
"Manually forward viewWillTransitionToSize:withTransitionCoordinator: if necessary");

//187.在iOS6之後,覆蓋此方法可以禁止掉旋轉回調自動傳遞的特性(因為有時候作為容器的childViewController可能不需要這些方法傳遞給所有的childViewControllers,只需要傳遞給正在顯示的childVieControllrer)





- (BOOL)shouldAutomaticallyForwardAppearanceMethods
NS_AVAILABLE_IOS(6_0);

//188.在iOS6之後需要覆蓋整個方法來關閉appearance callbacks的自動傳遞特性

 
/*

These two methods are public for container subclasses to call when transitioning between child

controllers. If they are overridden, the overrides should ensure to call the super. The parent argument in

both of these methods is nil when a child is being removed from its parent; otherwise it is equal to the new

parent view controller.



addChildViewController: will call [child willMoveToParentViewController:self] before adding the

child. However, it will not call didMoveToParentViewController:. It is expected that a container view

controller subclass will make this call after a transition to the new child has completed or, in the

case of no transition, immediately after the call to addChildViewController:. Similarly

removeFromParentViewController: does not call [self willMoveToParentViewController:nil] before removing the

child. This is also the responsibilty of the container subclass. Container subclasses will typically define

a method that transitions to a new child by first calling addChildViewController:, then executing a

transition which will add the new child's view into the view hierarchy of its parent, and finally will call

didMoveToParentViewController:. Similarly, subclasses will typically define a method that removes a child in

the reverse manner by first calling [child willMoveToParentViewController:nil].

*/

- (void)willMoveToParentViewController:(UIViewController *)parent
NS_AVAILABLE_IOS(5_0);

//189.當addChildViewController:接口建立了邏輯上的父子關系,接口的邏輯中會自動調用willMoveToParentViewController這個方法,在即將移動顯示ParentViewController的時候調用



- (void)didMoveToParentViewController:(UIViewController *)parent
NS_AVAILABLE_IOS(5_0);

//190.這個方法和上面這個方法類似,在移動顯示ParentViewController之後自動調用

 
@end

 
@interface UIViewController (UIStateRestoration) <UIStateRestoring>

//191.UIStateRestoration提供持久數據的接口

 
@property (nonatomic, copy) NSString *restorationIdentifier
NS_AVAILABLE_IOS(6_0);

//192.這個屬性是用來標示是否支持保存,恢復視圖狀態信息

 
@property (nonatomic, readwrite, assign) Class<UIViewControllerRestoration> restorationClass
NS_AVAILABLE_IOS(6_0);

//193.



- (void) encodeRestorableStateWithCoder:(NSCoder *)coder
NS_AVAILABLE_IOS(6_0);

//194.保存視圖狀態相關信息



- (void) decodeRestorableStateWithCoder:(NSCoder *)coder
NS_AVAILABLE_IOS(6_0);

//195.恢復和保持視圖狀態相關信息



- (void) applicationFinishedRestoringState
NS_AVAILABLE_IOS(7_0);

//196.其他對象解碼完成後,系統會調用此方法

 
@end

 
@interface UIViewController (UIConstraintBasedLayoutCoreMethods)

//197.增加分類基於布局約束的核心方法



/* Base implementation sends -updateConstraints to the view.

When a view has a view controller, this message is sent to the view controller during

the autolayout updateConstraints pass in lieu of sending updateConstraints directly

to the view.

You may override this method in a UIViewController subclass for updating custom

constraints instead of subclassing your view and overriding -[UIView updateConstraints].

Overrides must call super or send -updateConstraints to the view.

*/

- (void)updateViewConstraints
NS_AVAILABLE_IOS(6_0);

//198.更新布局約束的方法,viewController的view再更新視圖布局時,會先調用這個方法,可以通過重寫這個方法去更新當前view的內部布局,而不用再繼承這個view去重寫updateConstraints方法,記得注意重寫時,要調用super或者調用當前view的updateContraints方法。

 
@end

 
@protocol UIViewControllerTransitioningDelegate;

//199.viewController過渡的代理協議

 
@interface UIViewController(UIViewControllerTransitioning)

 
@property (nonatomic,assign)
id <UIViewControllerTransitioningDelegate> transitioningDelegate
NS_AVAILABLE_IOS(7_0);

//200.實例化協議屬性

 
@end

 
@interface UIViewController (UILayoutSupport)

// These objects may be used as layout items in the NSLayoutConstraint API
@property(nonatomic,readonly,retain)
id<UILayoutSupport> topLayoutGuide NS_AVAILABLE_IOS(7_0);

//201.這個屬性是通過計算viewController->view->top到覆蓋這個view最下層的那個bar-》bottom的距離

 
@property(nonatomic,readonly,retain)
id<UILayoutSupport> bottomLayoutGuide NS_AVAILABLE_IOS(7_0);

//202.這個屬性是通過計算viewController->view->bottom到覆蓋這個biew上層那個bar-top的距離

 
@end



@class NSExtensionContext;//????????????????????????????????????

//203.引入NSExtensionContext類,iOS8新增的一個跨站上下文屬性的extensionContext,來處理containing
app與擴展之間的通信,上下文的類型是NSExtensionContext。

 
@interface UIViewController(NSExtensionAdditions) <NSExtensionRequestHandling>

//204.添加分類
並且綁定協議



// Returns the extension context. Also acts as a convenience method for a view controller to check if it participating in an extension request.
@property (nonatomic,readonly,retain) NSExtensionContext *extensionContext
NS_AVAILABLE_IOS(8_0);

//205.實例化NSExtensionContext屬性

 
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved