一、打飛機(jī)游戲
項(xiàng)目:Homework_Plane_Teacher0309
1.1 添加標(biāo)簽 #pragma mark 1.創(chuàng)建兩張地圖,實(shí)現(xiàn)地圖的移動(dòng)
1.2 代碼的封裝
1.2.1 //TODO:
1.2.2 #pragma mark
二、視圖控制器
項(xiàng)目:ViewController0309
2.1 AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self.window rootViewController];
//視圖控制器的
//作用:管理視圖
//視圖控制器內(nèi)部 有一個(gè)自帶的view
//1.創(chuàng)建視圖控制器的一個(gè)子類
//2.實(shí)例化一個(gè)視圖控制器的對(duì)象
//firstVC:是一個(gè)抽象的管理者,不可見。
_firstVC = [[FirstViewController alloc]init];
//把firstVC設(shè)置為window的根視圖控制器,
//就相當(dāng)于將firstVC.view添加到window上
//即:[self.window addSubview:firstVC.view];
//視圖控制器加載view的模式:懶加載模式(當(dāng)你使用視圖控制器的時(shí)候,才會(huì)加載view)
self.window.rootViewController = _firstVC;
return YES;
}
2.2 FirstViewController.m
//視圖已經(jīng)加載成功
//注意:這個(gè)方法在視圖控制器的整個(gè)生命周期中只會(huì)調(diào)用一次
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 60, 40);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"下一頁" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
//下一頁按鈕
- (void)click:(UIButton *)button
{
SecondViewController *secondVC = [[SecondViewController alloc]init];
//找到window window是 APPDelegate類的屬性
//1.先找到“應(yīng)用程序類”(單例類)的對(duì)象
//即UIApplication的對(duì)象
//單例類:在此應(yīng)用程序中只有一個(gè)對(duì)象
UIApplication *app = [UIApplication sharedApplication];
//2.找入口類的對(duì)象
//即AppDelegate的對(duì)象
//就是UIApplication的對(duì)象的"代理協(xié)議"
//即app.delegate
AppDelegate *appDelegate = app.delegate;
//3.獲取window
UIWindow *window = appDelegate.window;
window.rootViewController = secondVC;
}
//只要view要顯示出來
//就會(huì)調(diào)用兩個(gè)方法:視圖將要顯示和視圖已經(jīng)顯示
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
//視圖已經(jīng)顯示
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
2.3 SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
//添加返回按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 60, 40);
btn.backgroundColor = [UIColor grayColor];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)click
{
FirstViewController *firstVC = [[FirstViewController alloc]init];
//1.找application
UIApplication *app = [UIApplication sharedApplication];
//2.獲得APPDelegate類對(duì)象
AppDelegate *appDelegate = app.delegate;
//3.獲取window
UIWindow *window = appDelegate.window;
//獲取FirstViewController
window.rootViewController = firstVC;
}