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

ioS UI-导航控制器(NavigationController)

时间:2015-12-30 21:40:59      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

  1 #import "AppDelegate.h"
  2 #import "ViewController.h"
  3 
  4 @interface AppDelegate ()
  5 
  6 @end
  7 
  8 @implementation AppDelegate
  9 
 10 
 11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 12     //1.窗口初始化
 13     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
 14     
 15     //2.视图控制器
 16     ViewController *vc = [[ViewController alloc] init];
 17     //VC.view.backgroundColor = [UIColor cyanColor];
 18     //[self.window makeKeyAndVisible];
 19 
 20     
 21     //3.创建一个导航控制器对象,并将rootVC作为导航控制器的根视图控制器
 22     UINavigationController *navCtr = [[UINavigationController alloc] initWithRootViewController:vc];
 23     //简易更改外观
 24     [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBg.png"] forBarMetrics:UIBarMetricsDefault];
 25     
 26     //4.将导航控制器设置为根视图控制器
 27     self.window.rootViewController =navCtr;
 28     //5.关键步骤:让当前窗口作为keyWindow(唯一性)并且可见
 29     [self.window makeKeyAndVisible];
 30     
 31     return YES;
 32 }
 33 
 34 
 35 #import "ViewController.h"
 36 #import "FirstViewController.h"
 37 @interface ViewController ()
 38 
 39 @end
 40 
 41 @implementation ViewController
 42 
 43 /*
 44  导航控制器->视图控制器->视图
 45  当根控制器设置为导航控制器时
 46  
 47  UINavigationController
 48  是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图。任何类型的视图控制器都可放入栈中。在设计导航控制器时需要指定根视图即用户看到的第一个视图。根视图控制器是被导航控制器推入到栈中的第一个视图控制器。当用户查看下一个试图时,栈中将加入一个新的视图控制器,它所控制的视图将展示给用户。我们可以通过导航按钮来操作分层的应用程序,用它来控制视图的推入或推出
 49  */
 50 
 51 - (void)viewDidLoad {
 52     [super viewDidLoad];
 53     self.title = @"ViewController";
 54     self.view.backgroundColor = [UIColor purpleColor];
 55     
 56     
 57     UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAdd)];
 58     self.navigationItem.rightBarButtonItem = rightBtn;
 59 
 60     
 61     UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
 62     btn.frame = CGRectMake(90, 90, 200, 50);
 63     [btn setTitle:@"Go to the next" forState:UIControlStateNormal];
 64     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 65     btn.backgroundColor = [UIColor whiteColor];
 66     [self.view addSubview:btn];
 67     
 68     [btn addTarget:self action:@selector(btnPushClick) forControlEvents:UIControlEventTouchUpInside];
 69     
 70 
 71 }
 72 
 73 #pragma mark - 自定义方法
 74 
 75 #pragma mark -UIBarButtonItem的右耳目关联方法
 76 - (void)myAdd
 77 {
 78     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Touch" message:@"添加某种功能" delegate:self cancelButtonTitle:@"Good Job" otherButtonTitles: nil];
 79     [alert show];
 80 }
 81 
 82 #pragma mark - 按钮关联方法-压栈
 83 - (void)btnPushClick
 84 {
 85     //看到的是栈顶视图,想看下一个必需把它压进来
 86     FirstViewController *firstVC = [[FirstViewController alloc] init];
 87     [self.navigationController pushViewController:firstVC animated:YES];
 88 }
 89 
 90 - (void)didReceiveMemoryWarning {
 91     [super didReceiveMemoryWarning];
 92     // Dispose of any resources that can be recreated.
 93 }
 94 
 95 @end
 96 
 97 
 98 
 99 #import "FirstViewController.h"
101 
102 @interface FirstViewController ()
103 
104 @end
105 
106 @implementation FirstViewController
107 
108 - (void)viewDidLoad {
109     [super viewDidLoad];
110     self.title = @"FirstViewController";
111     self.view.backgroundColor = [UIColor lightGrayColor];
112     
113     UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
114     btn.frame = CGRectMake(90, 90, 200, 50);
115     [btn setTitle:@"Go to the next" forState:UIControlStateNormal];
116     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
117     btn.backgroundColor = [UIColor whiteColor];
118     [self.view addSubview:btn];
119     
120     [btn addTarget:self action:@selector(btnPushClick) forControlEvents:UIControlEventTouchUpInside];
121     
122     
123 }
124 #pragma mark - 自定义方法
125 #pragma mark - 按钮关联方法
126 - (void)btnPushClick
127 {
128     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrautulations" message:@"当前为最后一页" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
129     [alert show];
130 
131 }
132 
133 
134 - (void)didReceiveMemoryWarning {
135     [super didReceiveMemoryWarning];
136     // Dispose of any resources that can be recreated.
137 }
138 
139 
140 @end

 

ioS UI-导航控制器(NavigationController)

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5089895.html

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