试图控制器。生命周期UIViewController

#import "AppDelegate.h"

#import "RootViewController.h"


@implementation AppDelegate


- (void)dealloc{

    [_array release];

    [super dealloc];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    _array = [[NSMutableArray alloc] initWithObjects:@"4",@"后会无期",nil];

    // Override point for customization after application launch.

    //得到vc对象 但此时vc.view是没有

    RootViewController *root = [[RootViewController alloc] init];

    self.window.rootViewController = root;

    [root release];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}


- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end


********************************************************

#import "RootViewController.h"

#import "SubViewController.h"

#import "AppDelegate.h"


@interface RootViewController ()

{

    NSMutableArray *_dataArray;

}

@end


@implementation RootViewController


//视图控制器view的生命周期,非常重要

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

            NSLog(@"%s",__FUNCTION__);

        //可以完成对数据对象的初始化

        _dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",nil];

    }

    return self;

}


- (id)init{

    self = [super init];

    if (self) {

        NSLog(@"%s",__FUNCTION__);

    }

    return self;

}



/***一般情况下只调用一次**/

//view属性第一次被使用时,vc会自动通过loadView方法来创建self.view

- (void)loadView{

    [super loadView];//重写loadView方法,必须手动调用super loadView,让UIViewController创建一个view

    //__FUNCTION__ 打印当前方法的名称

    NSLog(@"%s",__FUNCTION__);

}


//将视图加载到内存中

- (void)viewDidLoad

{

    [super viewDidLoad];//UIViewController 对一些变量进行必要的初始化操作

     NSLog(@"%s",__FUNCTION__);

    NSLog(@"array:%@",_dataArray);

    //将操作view的代码写在此处

    //添加子视图的操作,和对变量的初始化代码也可以写在此处

    self.view.backgroundColor = [UIColor yellowColor];

// Do any additional setup after loading the view.

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"change" forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,30,300,50)];

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    //拿到AppDelegate单例

    //sharedApplication 拿到应用程序对象的单例

    //delegate属性拿到AppDelegate单例

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    [delegate.array addObject:@"小时代3"];

}


- (void)btnClicked:(UIButton *)btn{

    SubViewController *sub = [[SubViewController alloc] init];

    //通过模态化的方式,将sub对象的视图弹出(呈现出来)

    //completion-block(匿名函数)

    //sub 引用计数+1

    //相当于将rootviewwindow上移除,将subview添加到window

    //设置视图动画的切换方式

    sub.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentViewController:sub animated:YES completion:^{

        //当动画结束后,调用此处的代码

        NSLog(@"finished!!");

    }];

    [sub release];

}



/***可以被调用多次**/

//视图即将出现的时候,调用此方法

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//视图已经出现后,调用此方法

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//视图即将消失的时候,调用此方法

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//已经消失

- (void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];

    NSLog(@"%s",__FUNCTION__);

}


//当程序开辟的活跃内存过多,操作系统向程序发出内存告急的信号时,会触发vc的此方法

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//vc对象被销毁之前,会自动销毁self.view

- (void)dealloc{

    [_dataArray release];//释放成员变量

    [super dealloc];

}


@end

#import "RootViewController.h"

#import "SubViewController.h"

#import "AppDelegate.h"


@interface RootViewController ()

{

    NSMutableArray *_dataArray;

}

@end


@implementation RootViewController


//视图控制器view的生命周期,非常重要

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

            NSLog(@"%s",__FUNCTION__);

        //可以完成对数据对象的初始化

        _dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",nil];

    }

    return self;

}


- (id)init{

    self = [super init];

    if (self) {

        NSLog(@"%s",__FUNCTION__);

    }

    return self;

}



/***一般情况下只调用一次**/

//view属性第一次被使用时,vc会自动通过loadView方法来创建self.view

- (void)loadView{

    [super loadView];//重写loadView方法,必须手动调用super loadView,让UIViewController创建一个view

    //__FUNCTION__ 打印当前方法的名称

    NSLog(@"%s",__FUNCTION__);

}


//将视图加载到内存中

- (void)viewDidLoad

{

    [super viewDidLoad];//UIViewController 对一些变量进行必要的初始化操作

     NSLog(@"%s",__FUNCTION__);

    NSLog(@"array:%@",_dataArray);

    //将操作view的代码写在此处

    //添加子视图的操作,和对变量的初始化代码也可以写在此处

    self.view.backgroundColor = [UIColor yellowColor];

// Do any additional setup after loading the view.

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"change" forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,30,300,50)];

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    //拿到AppDelegate单例

    //sharedApplication 拿到应用程序对象的单例

    //delegate属性拿到AppDelegate单例

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    [delegate.array addObject:@"小时代3"];

}


- (void)btnClicked:(UIButton *)btn{

    SubViewController *sub = [[SubViewController alloc] init];

    //通过模态化的方式,将sub对象的视图弹出(呈现出来)

    //completion-block(匿名函数)

    //sub 引用计数+1

    //相当于将rootviewwindow上移除,将subview添加到window

    //设置视图动画的切换方式

    sub.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentViewController:sub animated:YES completion:^{

        //当动画结束后,调用此处的代码

        NSLog(@"finished!!");

    }];

    [sub release];

}



/***可以被调用多次**/

//视图即将出现的时候,调用此方法

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//视图已经出现后,调用此方法

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//视图即将消失的时候,调用此方法

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    NSLog(@"%s",__FUNCTION__);

}

//已经消失

- (void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];

    NSLog(@"%s",__FUNCTION__);

}


//当程序开辟的活跃内存过多,操作系统向程序发出内存告急的信号时,会触发vc的此方法

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//vc对象被销毁之前,会自动销毁self.view

- (void)dealloc{

    [_dataArray release];//释放成员变量

    [super dealloc];

}


@end



***********************************************************

#import "SubViewController.h"

#import "AppDelegate.h"


@interface SubViewController ()


@end


@implementation SubViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    /*MVC 是一种软件架构级的设计模式

     *m-model 数据模型 负责数据的操作和存储

     *v-view  视图  负责展示数据,以及与用户交互

     *c-controller 控制器 负责将modelview联系起来:负责一些处理数据的逻辑,负责将数据给到view 并负责view的刷新,以及相应viewdelegate方法等

     */

    self.view.backgroundColor = [UIColor cyanColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [btn setTitle:@"dismiss" forState:UIControlStateNormal];

    [btn setFrame:CGRectMake(10,30,300,50)];

    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    NSLog(@"array:%@",delegate.array);

    

// Do any additional setup after loading the view.

}


- (void)btnClicked:(UIButton *)btn{

    //返回到之前的状态

    //dismissViewControllerAnimated   self引用计数-1

    [self dismissViewControllerAnimated:YES completion:^{

    }];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值