#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
//相当于将root的view从window上移除,将sub的view添加到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
//相当于将root的view从window上移除,将sub的view添加到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 控制器 负责将model和view联系起来:负责一些处理数据的逻辑,负责将数据给到view 并负责view的刷新,以及相应view的delegate方法等
*/
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