美文网首页OC技术iOS-OC初级
iOS应用开发实战(7-9)-ViewController &a

iOS应用开发实战(7-9)-ViewController &a

作者: 逸飞u | 来源:发表于2016-03-09 13:12 被阅读404次

这3节课互相关联性较强,因此把笔记放一起了。
有关调试的内容放在其它笔记里

MVC(Model-View-Controller)

是一种应用程序设计框架

MVC.jpg App Overview.png

补充阅读:

https://en.wikipedia.org/wiki/Model–view–controller

主流设计模式简介

主流设计模式.jpg

UIViewController

作用:

管理View的Controller

具体作用:

  • 管理View:链接占位
  • 传递数据:链接占位
  • 响应用户操作:链接占位
  • 内存管理、状态标尺:链接占位
  • 适应设备:链接占位

UIViewController示意图

UIViewController.jpg

创建初始化界面

创建初始化界面.jpg

空界面图示

空界面图示.jpg

Tips:View debug的路径,注意:需要先运行模拟器

Capture View Hierarchy.png

View Controller Lifecycle

UIViewController Class Reference_2x.png

多View Controller常用方法和属性

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;

//View风格
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle;

//风格枚举
typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
            UIModalPresentationFullScreen = 0,
            UIModalPresentationPageSheet,
            UIModalPresentationFormSheet ,
            UIModalPresentationCurrentContext ,
            UIModalPresentationCustom ,
            UIModalPresentationOverFullScreen ,
            UIModalPresentationOverCurrentContext ,
            UIModalPresentationPopover ,
            UIModalPresentationNone  = -1,         
};

//动画风格
@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle;

//风格枚举
typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
    UIModalTransitionStyleCoverVertical = 0,
    UIModalTransitionStyleFlipHorizontal,
    UIModalTransitionStyleCrossDissolve,
    UIModalTransitionStylePartialCurl ,
};
//显示
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;

//撤销
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion ;//全部撤销,数据会丢失

UIViewController的属性和方法一览(占位)

- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecode;

@property(null_resettable, nonatomic,strong) UIView *view;
- (void)loadView;
- (void)loadViewIfNeeded;

@property(nullable, nonatomic, readonly, strong) UIView *viewIfLoaded;

- (void)viewDidLoad;
- (BOOL)isViewLoaded;

@property(nullable, nonatomic, readonly, copy) NSString *nibName;
@property(nullable, nonatomic, readonly, strong) NSBundle *nibBundle;
@property(nullable, nonatomic, readonly, strong) UIStoryboard *storyboard;

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender;
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender;
- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sende;
- (NSArray<UIViewController *> *)allowedChildViewControllersForUnwindingFromSource:(UIStoryboardUnwindSegueSource *)source;
- (nullable UIViewController *)childViewControllerContainingSegueSource:(UIStoryboardUnwindSegueSource *)source ;
- (void)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC;
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillLayoutSubviews;
- (void)viewDidLayoutSubviews;

@property(nullable, nonatomic,copy) NSString *title;
- (void)didReceiveMemoryWarning;

@property(nullable,nonatomic,weak,readonly) UIViewController *parentViewController;
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController;
@property(nullable, nonatomic,readonly) UIViewController *presentingViewControlle;
@property(nonatomic,assign) BOOL definesPresentationContext;
@property(nonatomic,assign) BOOL providesPresentationContextTransitionStyle;

- (BOOL)isBeingPresented;
- (BOOL)isBeingDismissed;
- (BOOL)isMovingToParentViewController;
- (BOOL)isMovingFromParentViewController;
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion ;

@property(nonatomic,assign) UIModalTransitionStyle modalTransitionStyle;
@property(nonatomic,assign) UIModalPresentationStyle modalPresentationStyle;
@property(nonatomic,assign) BOOL modalPresentationCapturesStatusBarAppearance;

- (BOOL)disablesAutomaticKeyboardDismissal;

@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout;
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars;
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets;
@property (nonatomic) CGSize preferredContentSize;

- (UIStatusBarStyle)preferredStatusBarStyle
- (BOOL)prefersStatusBarHidden;
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation;
- (void)setNeedsStatusBarAppearanceUpdate ;
- (nullable UIViewController *)targetViewControllerForAction:(SEL)action sender:(nullable id)sender ;
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;
- (void)showDetailViewController:(UIViewController *)vc sender:(nullable id)sender;

Demo:helloStoryBoard

helloStoryBoard.jpg

Storyboard Reference 位置

Storyboard Refa.png

跳转和传值

//跳转
[self showViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"vc2"] sender:sender];

//用代码执行Segue
-(IBAction)actionWithSegue:(id)sender{
    [self performSegueWithIdentifier:@"go2" sender:sender];
}

//Go Scene 2
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
   if ([segue.identifier isEqualToString:@"go2"]) {    
        UIViewController *destination = segue.destinationViewController;      
        [destination setValue:_value forKey:@"value"];
    }
}
//后退到本Scene
-(IBAction)unwindToThisScene:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC{
 //判断来源     
    if([unwindSegue.sourceViewController isKindOfClass:[secondViewController class]]){

        secondViewController *sc =(secondViewController *)unwindSegue.sourceViewController;
        _value = sc.value;
}
    else  if([unwindSegue.sourceViewController isKindOfClass:[ThirdViewController class]]){
 //       self.textField.text = nil;
    }
}

App Lifecycle

启动

启动.jpg

互动

互动.jpg

委托

委托.jpg

状态切换

状态切换.jpg

相关文章

网友评论

    本文标题:iOS应用开发实战(7-9)-ViewController &a

    本文链接:https://www.haomeiwen.com/subject/agwzkttx.html