码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习笔记(3)--初识UINavigationController(无storyboard)

时间:2015-03-21 06:20:02      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

纯代码创建导航控制器UINavigationController

在Xcode6.1中创建single view application的项目,删除Main.storyboard文件,删除info.plist中main storyboard相关属性,依靠代码编写UI视图,研究导航控制器栈的原理。

AppDelegate.h文件代码

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 
5 @property (strong, nonatomic) UIWindow *window;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     // Override point for customization after application launch.
12     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
13     ViewController *firstView = [[ViewController alloc] init];
14     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstView];
15     [nav.navigationController pushViewController:firstView animated:YES];
16     self.window.rootViewController = nav;
17     self.window.backgroundColor = [UIColor whiteColor];
18     [self.window makeKeyAndVisible];
19     
20     return YES;
21 }
22 
23 - (void)applicationWillResignActive:(UIApplication *)application {
24     // 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.
25     // 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.
26 }
27 
28 - (void)applicationDidEnterBackground:(UIApplication *)application {
29     // 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.
30     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31 }
32 
33 - (void)applicationWillEnterForeground:(UIApplication *)application {
34     // 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.
35 }
36 
37 - (void)applicationDidBecomeActive:(UIApplication *)application {
38     // 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.
39 }
40 
41 - (void)applicationWillTerminate:(UIApplication *)application {
42     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43 }
44 
45 @end

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 
5 - (IBAction)goToSecondView:(id)sender;
6 @end

ViewController.m

 1 #import "ViewController.h"
 2 #import "SecondViewController.h"
 3 
 4 @interface ViewController ()
 5 
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view, typically from a nib.
13     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 40)];
14     label.text = @"This the first view";
15     [self.view addSubview:label];
16     UIButton *nextView = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 200, 40)];
17     [nextView setTitle:@"Next View" forState:UIControlStateNormal];
18     [nextView setBackgroundColor:[UIColor blueColor]];
19     [nextView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
20     [nextView addTarget:self action:@selector(goToSecondView:) forControlEvents:UIControlEventTouchDown];
21     [self.view addSubview:nextView];
22     
23 }
24 
25 
26 
27 - (IBAction)goToSecondView:(id)sender {
28     SecondViewController *second = [[SecondViewController alloc] init];
29     [self.navigationController pushViewController:second animated:YES];
30 //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"123" message:@"321" delegate:nil cancelButtonTitle:@"111" otherButtonTitles: nil];
31 //    [alert show];
32     NSLog(@"click");
33 }
34 
35 - (void)didReceiveMemoryWarning {
36     [super didReceiveMemoryWarning];
37     // Dispose of any resources that can be recreated.
38 }
39 
40 @end

SecondViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface SecondViewController : UIViewController
4 - (IBAction)goToThird:(id)sender;
5 @end
 1 #import "SecondViewController.h"
 2 #import "ThirdViewController.h"
 3 
 4 @interface SecondViewController ()
 5 
 6 @end
 7 
 8 @implementation SecondViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.
13     UIButton *nextView = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 200, 40)];
14     [nextView setTitle:@"Next View" forState:UIControlStateNormal];
15     [nextView setBackgroundColor:[UIColor blueColor]];
16     [nextView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
17     [nextView addTarget:self action:@selector(goToThird:) forControlEvents:UIControlEventTouchDown];
18     [self.view addSubview:nextView];
19 }
20 
21 - (void)viewDidAppear:(BOOL)animated {
22     UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 200, 40)];
23     secondLabel.text = @"This is the second view";
24     [self.view addSubview:secondLabel];
25 }
26 
27 - (IBAction)goToThird:(id)sender {
28     ThirdViewController *third = [[ThirdViewController alloc] init];
29     [self.navigationController pushViewController:third animated:YES];
30 }
31 
32 - (void)didReceiveMemoryWarning {
33     [super didReceiveMemoryWarning];
34     // Dispose of any resources that can be recreated.
35 }
36 
37 /*
38 #pragma mark - Navigation
39 
40 // In a storyboard-based application, you will often want to do a little preparation before navigation
41 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
42     // Get the new view controller using [segue destinationViewController].
43     // Pass the selected object to the new view controller.
44 }
45 */
46 
47 @end

ThirdViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ThirdViewController : UIViewController
4 
5 @end

ThirdViewController.m

 1 #import "ThirdViewController.h"
 2 
 3 @interface ThirdViewController ()
 4 
 5 @end
 6 
 7 @implementation ThirdViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view.
12 }
13 
14 - (void)didReceiveMemoryWarning {
15     [super didReceiveMemoryWarning];
16     // Dispose of any resources that can be recreated.
17 }
18 
19 - (void)viewDidAppear:(BOOL)animated {
20     UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 120, 200, 40)];
21     secondLabel.text = @"This is the third view";
22     [self.view addSubview:secondLabel];
23 }
24 
25 /*
26 #pragma mark - Navigation
27 
28 // In a storyboard-based application, you will often want to do a little preparation before navigation
29 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
30     // Get the new view controller using [segue destinationViewController].
31     // Pass the selected object to the new view controller.
32 }
33 */
34 
35 @end

 

iOS学习笔记(3)--初识UINavigationController(无storyboard)

标签:

原文地址:http://www.cnblogs.com/nycoder/p/4355059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!