OC--UIViewController基礎(chǔ)知識(shí)整理

alloc init

//這個(gè) 方法是在viewdidload之前加載的,以nib文件加載必然會(huì)調(diào)用此方法,但是代碼加載也是會(huì)自動(dòng)調(diào)用的,因此可以將一些數(shù)組創(chuàng)建,標(biāo)題命名等初始化操作寫在這里
//1.如果在初始化UIViewController指定了xib文件名,就會(huì)根據(jù)傳入的xib文件名加載對(duì)應(yīng)的xib文件
//2.如果沒(méi)有明顯地傳xib文件名([[MJViewController alloc] init];),就會(huì)加載跟UIViewController同名的xib文件
//3.如果沒(méi)有找到相關(guān)聯(lián)的xib文件,就會(huì)創(chuàng)建一個(gè)空白的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;
//這個(gè)UIViewController的nib名字
@property(nullable, nonatomic, readonly, copy) NSString *nibName;
//這個(gè)UIViewController的nibBundle
@property(nullable, nonatomic, readonly, strong) NSBundle *nibBundle;
//這個(gè)UIViewControlle所在的Storyboar
@property(nullable, nonatomic, readonly, strong) UIStoryboard *storyboard NS_AVAILABLE_IOS(5_0);

loadView

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

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

//iOS9之前
//有些時(shí)候因?yàn)樾枰謩?dòng)調(diào)用loadview
//但是有風(fēng)險(xiǎn),系統(tǒng)不再調(diào)用viewDidLoad
//所以手動(dòng)調(diào)用loadview是錯(cuò)誤的

//iOS9之后
//出現(xiàn)了loadViewIfNeeded解決了這個(gè)問(wèn)題
//調(diào)用這個(gè)方法視圖會(huì)創(chuàng)建出來(lái)并且不會(huì)忽略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相關(guān)

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

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


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

//Unwind Segue相關(guān),(不明白,留著慢慢玩)
- (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;
//這幾個(gè)生命周期方法,必須熟悉
- (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;

單個(gè)viewController的生命周期

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

多個(gè)viewControllers跳轉(zhuǎn)

當(dāng)我們點(diǎn)擊push的時(shí)候首先會(huì)加載下一個(gè)界面然后才會(huì)調(diào)用界面的消失方法
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標(biāo)題
@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屬性默認(rèn)值是UIRectEdgeAll,指定控制器將它的視圖延伸到屏幕的邊緣并在bar下面。如果屬性值為:UIRectEdgeNone,控制器視圖遇到bar的邊界就不延伸了。
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout ; // Defaults to UIRectEdgeAll

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

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

UIStatusBar相關(guān)的設(shè)置iOS-UIStatusBar詳細(xì)總結(jié)

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

UIModalTransitionStyle是彈出模態(tài)ViewController時(shí)的四種動(dòng)畫(huà)風(fēng)格

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

UIModalPresentationStyle是彈出模態(tài)ViewController時(shí)彈出風(fēng)格
Present ViewController詳解

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

如何present一個(gè)半透明的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對(duì)這組協(xié)議提供了默認(rèn)的實(shí)現(xiàn),我們自定義ViewConttroller的時(shí)候可以重寫這些方法來(lái)調(diào)整視圖布局。(先放放,等有時(shí)間在研究使用)

@protocol UIContentContainer <NSObject>

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

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

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

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

//TransitionToSize:動(dòng)畫(huà)方面觸發(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容