OC--UIViewController基礎知識整理

alloc init

//這個 方法是在viewdidload之前加載的,以nib文件加載必然會調用此方法,但是代碼加載也是會自動調用的,因此可以將一些數(shù)組創(chuàng)建,標題命名等初始化操作寫在這里
//1.如果在初始化UIViewController指定了xib文件名,就會根據(jù)傳入的xib文件名加載對應的xib文件
//2.如果沒有明顯地傳xib文件名([[MJViewController alloc] init];),就會加載跟UIViewController同名的xib文件
//3.如果沒有找到相關聯(lián)的xib文件,就會創(chuàng)建一個空白的UIView,然后賦值給UIViewController的view屬性
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil NS_DESIGNATED_INITIALIZER;
//NSCoding協(xié)議
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//這個UIViewController的nib名字
@property(nullable, nonatomic, readonly, copy) NSString *nibName;
//這個UIViewController的nibBundle
@property(nullable, nonatomic, readonly, strong) NSBundle *nibBundle;
//這個UIViewControlle所在的Storyboar
@property(nullable, nonatomic, readonly, strong) UIStoryboard *storyboard NS_AVAILABLE_IOS(5_0);

loadView

//如果獲取self.view,如果沒有現(xiàn)實[self loadView],就是調用Subclasses的loadView方法。
@property(null_resettable, nonatomic,strong) UIView *view;

//loadView里面自定義self.view
- (void)loadView;

//iOS9之前
//有些時候因為需要手動調用loadview
//但是有風險,系統(tǒng)不再調用viewDidLoad
//所以手動調用loadview是錯誤的

//iOS9之后
//出現(xiàn)了loadViewIfNeeded解決了這個問題
//調用這個方法視圖會創(chuàng)建出來并且不會忽略viewDidLoad
- (void)loadViewIfNeeded NS_AVAILABLE_IOS(9_0);

//返回加載完的self.view
@property(nullable, nonatomic, readonly, strong) UIView *viewIfLoaded NS_AVAILABLE_IOS(9_0);

//view是否加載完成
@property(nonatomic, readonly, getter=isViewLoaded) BOOL viewLoaded ;
- (BOOL)isViewLoaded ;

Storyboary相關

//Storyboar跳轉擺好的界面
//identifier:設置好的界面連線(就是Storyboar中VC與VC那條線)
//sender:發(fā)送者
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender NS_AVAILABLE_IOS(5_0);

//看看能不能跳轉,主要是看identifier對不對
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender NS_AVAILABLE_IOS(6_0);


//準備處理,
//segue:用來判斷是哪個segue,里面有identifier、sourceViewController、destinationViewController可以各種操作
//sender:發(fā)送者
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender NS_AVAILABLE_IOS(5_0);

//Unwind Segue相關,(不明白,留著慢慢玩)
- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender NS_AVAILABLE_IOS(6_0);
- (NSArray<UIViewController *> *)allowedChildViewControllersForUnwindingFromSource:(UIStoryboardUnwindSegueSource *)source NS_AVAILABLE_IOS(9_0);
- (nullable UIViewController *)childViewControllerContainingSegueSource:(UIStoryboardUnwindSegueSource *)source NS_AVAILABLE_IOS(9_0);
- (nullable UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(nullable id)sender NS_DEPRECATED_IOS(6_0, 9_0);
- (void)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC NS_AVAILABLE_IOS(9_0);
- (nullable UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(nullable NSString *)identifier NS_DEPRECATED_IOS(6_0, 9_0);

生命周期函數(shù)

//系統(tǒng)的loadview完成后,執(zhí)行viewDidLoad
- (void)viewDidLoad;
//這幾個生命周期方法,必須熟悉
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);
- (void)didReceiveMemoryWarning;

單個viewController的生命周期

1、initWithCoder:(NSCoder *)aDecoder:(如果使用storyboard或者xib)
2、loadView:加載view
3、viewDidLoad:view加載完畢
4、viewWillAppear:控制器的view將要顯示
5、viewWillLayoutSubviews:控制器的view將要布局子控件
6、viewDidLayoutSubviews:控制器的view布局子控件完成,這期間系統(tǒng)可能會多次調用viewWillLayoutSubviews 、    viewDidLayoutSubviews 倆個方法
7、viewDidAppear:控制器的view完全顯示
8、viewWillDisappear:控制器的view即將消失的時候
9、這期間系統(tǒng)也會調用viewWillLayoutSubviews 、viewDidLayoutSubviews 兩個方法
10、viewDidDisappear:控制器的view完全消失的時候

多個viewControllers跳轉

當我們點擊push的時候首先會加載下一個界面然后才會調用界面的消失方法
1、initWithCoder:(NSCoder *)aDecoder:ViewController2 (如果用xib創(chuàng)建的情況下)
2、loadView:ViewController2
3、viewDidLoad:ViewController2
4、viewWillDisappear:ViewController1 將要消失
5、viewWillAppear:ViewController2 將要出現(xiàn)
6、viewWillLayoutSubviews ViewController2
7、viewDidLayoutSubviews ViewController2
8、viewWillLayoutSubviews:ViewController1
9、viewDidLayoutSubviews:ViewController1
10、viewDidDisappear:ViewController1 完全消失
11、viewDidAppear:ViewController2 完全出現(xiàn)
//nav標題
@property(nullable, nonatomic,copy) NSString *title;

@property(nullable,nonatomic,weak,readonly) UIViewController *parentViewController;//父控制器
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController;//第二者
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController;//第一者

//例子
UIViewController *vcB = [[UIViewController alloc] init];
[self addChildViewController:vcB];
NSLog(@"%@", vcB.parentViewController);//self

UIViewController *vcB = [[UIViewController alloc] init];
[self presentViewController:vcB animated:YES completion:^{}];
    
NSLog(@"%@", self.presentedViewController);//vcB
NSLog(@"%@", self.presentingViewController);//nil
    
NSLog(@"%@", vcB.presentedViewController);//nil
NSLog(@"%@", vcB.presentingViewController);//self

//UIViewController的edgesForExtendedLayout屬性默認值是UIRectEdgeAll,指定控制器將它的視圖延伸到屏幕的邊緣并在bar下面。如果屬性值為:UIRectEdgeNone,控制器視圖遇到bar的邊界就不延伸了。
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout ; // Defaults to UIRectEdgeAll

//UIViewController的extendedLayoutIncludesOpaqueBars屬性可以控制以上屬性的有效性,默認值為NO,指定edgesForExtendedLayout在遇到不透明的bar時無效,即不延展。設置值為YES,則在遇到透明或不透明的bar情況下都會延展。
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars ; // Defaults to NO, but bars are translucent by default on 7_0.

//UIViewController的automaticallyAdjustsScrollViewInsets默認為YES,指定控制器在有UIScrollView及其子類并且在有導航欄或工具欄或標簽欄情況下,會自動調整其contentInset屬性。如果是導航欄contentInset.top = 64,如果是標簽欄contentInset.bottom = 44.
//可以將該屬性設置為NO,取消這種行為。
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets ; // Defaults to YES

UIStatusBar相關的設置iOS-UIStatusBar詳細總結

- (UIStatusBarStyle)preferredStatusBarStyle ;
- (BOOL)prefersStatusBarHidden ;
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation

UIModalTransitionStyle是彈出模態(tài)ViewController時的四種動畫風格

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,//是從底部滑入,默認
    UIModalTransitionStyleFlipHorizontal ,  //是水平翻轉
    UIModalTransitionStyleCrossDissolve,    //是交叉溶解
    UIModalTransitionStylePartialCurl,      //是翻書效果
};

UIModalPresentationStyle是彈出模態(tài)ViewController時彈出風格
Present ViewController詳解

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,      //是彈出VC時,VC充滿全屏
    UIModalPresentationPageSheet ,          //是如果設備橫屏,VC的顯示方式則從橫屏下方開始
    UIModalPresentationFormSheet ,          //是VC顯示都是從底部,寬度和屏幕寬度一樣
    UIModalPresentationCurrentContext ,     //是VC的彈出方式和VC父VC的彈出方式相同
    UIModalPresentationCustom ,             //自定義
    UIModalPresentationOverFullScreen ,     //
    UIModalPresentationOverCurrentContext , //
    UIModalPresentationPopover ,            //
    UIModalPresentationNone ,               //
};

如何present一個半透明的ViewController

- (void)btnOnClick:(id)sender {
    UIViewController *test = [[UIViewController alloc] init];
    self.definesPresentationContext = YES;
    test.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];
    test.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:test animated:YES completion:nil];
}

UIContentContainer
iOS8之后,加入了新的一組協(xié)議,UIViewController對這組協(xié)議提供了默認的實現(xiàn),我們自定義ViewConttroller的時候可以重寫這些方法來調整視圖布局。(先放放,等有時間在研究使用)

@protocol UIContentContainer <NSObject>

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

//當一個容器ViewController的ChildViewController的preferredContentSize值改變時,UIKit會調用這個方法告訴當前容器ViewController。我們可以在這個方法里根據(jù)新的Size對界面進行調整。
- (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);

//當一個容器ViewController的ChildViewController的systemLayoutFittingSize值改變時,UIKit會調用這個方法告訴當前容器ViewController。我們可以在這個方法里根據(jù)新的Size對界面進行調整。
- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);

//viewWillTransitionToSize:withTransitionCoordinator:調用的時候
- (CGSize)sizeForChildContentContainer:(id <UIContentContainer>)container withParentContainerSize:(CGSize)parentSize NS_AVAILABLE_IOS(8_0);

//TransitionToSize:動畫方面觸發(fā)?
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

//什么鬼啊?
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • /* UIViewController is a generic controller base class th...
    DanDanC閱讀 1,862評論 0 2
  • 27、ViewController的didReceiveMemoryWarning是在什么時候調用的?默認的操作是...
    煙雨平生花飛舞閱讀 623評論 0 1
  • 廢話不多說,直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,441評論 0 15
  • 麻將,是一種四人參加的游戲,各個地方有各個地方的打麻將規(guī)則,牌數(shù)也不大一樣。據(jù)說打麻將這一風氣盛行于明朝,相傳是一...
    木易每文閱讀 1,255評論 0 2
  • 言凡 一個人, 獨步于鄉(xiāng)間小路,夜的帷幕悄無聲息地輕輕滑落,周圍安靜的似乎沒有一點聲音。 漸行漸遠, 心,也變得寧...
    b825aefd4d93閱讀 290評論 0 0