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

纯代码创建导航控制器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
时间: 2024-10-03 13:46:39

iOS学习笔记(3)--初识UINavigationController(无storyboard)的相关文章

IOS学习笔记 -- Modal和Quartz2D

一. Modal1.Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止;Modal只是改变了View的现实,没有改变rootViewController 2.常用方法1>.以Modal的形式展示控制器- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion2>.关

iOS学习笔记(3)— 屏幕旋转

iOS学习笔记(3)— 屏幕旋转 一.屏幕旋转机制: iOS通过加速计判断当前的设备方向和屏幕旋转.当加速计检测到方向变化的时候,屏幕旋转的流程如下: 1.设备旋转时,系统接收到旋转事件. 2.系统将旋转事件通过AppDelegate通知当前的主Window. 3.window通知它的rootViewController. 4.rootViewController判断所支持的旋转方向,完成旋转. iOS系统中屏幕旋转事件没有像触碰事件那样进行hitTest,所以只有rootViewControl

iOS学习笔记-精华整理

iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放池就会被释放掉(调用dealloc),池中的对象都会收到一个release,有可能会因此被销毁. 2-成员属性:     readonly:不指定readonly,默认合成getter和setter方法.外界毫不关心的成员,则不要设置任何属性,这样封装能增加代码的独立性和安全

黑马程序员--IOS学习笔记--数组及排序

IOS学习笔记 概述: 8_2.改变整型变量的符号 8_2.改变整型变量所占存储空间 8_3.char类型数据存储 8_4.数组的基本概念及分类 8_5.数组元素作为函数参数 8_5.一维数组定义及注意事项 8_6.一维数组初始化 8_7.一维数组一个让人疑惑的问题 8_8.一维数组的引用 8_9.应用:数组遍历 8_10.一维数组的存储方式 8_11.一维数组的地址 8_12.一维数组长度计算方法 8_13.一维数组的越界问题 8_14.应用:找最大值 8_15.数组元素作为函数参数 8_16

【Smallfan的ios学习笔记】卷首引

[关于自己] 似乎先要自我介绍一下.首先我是一名三流专科院校的工科男生,所学的专业是计算机信息管理.计算机信息管理这个专业,说实话当时填报也并不了解,就冲着学校专业简介上面写着oracle开发运维而去,结果上天跟我开了个玩笑,这个专业分了两个方向,一个是系统开发与运用(SQL方向),一个是数据库运维与应用(oracle方向),很荣幸就被分到了SQL方向.一进这个专业,第一件事就是查询这三年将会开什么课程,课表如下: 简要筛选一下主要课程就是JAVA编程基础.JSP程序设计.JavaScript.

iOS学习笔记20-地图(二)MapKit框架

一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种方式可以对地图进行精准的控制 调用苹果官方自带的地图应用,主要用于一些简单的地图应用,无法精确控制 使用第三方地图开发SDK库 用得最多的还是MapKit,所以这节就只讲MapKit的使用. 二.MapKit核心类 MapKit的核心类为地图展示控件MKMapView,以下是常用的属性.对象方法以及

iOS学习笔记---c语言第六天

函数  function 命名规范:工程名第一个字母大写,变量函数名小写,不要用拼音和中文, eg:lessonFunction 一.函数声明定义 函数是具有特定功能的代码块        作用:模块化程序 用函数的好处:简化程序.提高开发效率.方便阅读和修改等 函数定义: 返回值类型  函数名(形式参数) { 语句 return 返回值: }     返回值类型:viod  int  char float.....    void下面可以不写return    ,int  下面返回值为整型,c

iOS学习笔记22-推送通知

一.推送通知 推送通知就是向用户推送一条信息来通知用户某件事件,可以在应用退到后台后,或者关闭后,能够通过推送一条消息通知用户某件事情,比如版本更新等等. 推送通知的常用应用场景: 一些任务管理APP,会到任务时间即将到达时,通知你该做任务了. 健身APP定时提醒你应该健身了. 买过电影票,提前半个小时通知你,电影即将开场. 当你QQ或微信收到信息时,即使退到后台,也可以收到信息通知你. 电商APP,推送一条消息通知我们有新品上架等等. 推送通知的常用展示样式: 屏幕顶部显示一块横幅 在锁屏界面

iOS学习笔记之UITableViewController&amp;UITableView

iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论文,因此需要继续思考新的算法.这是一件挺痛苦的事情,特别是在很难找到与自己研究方向相关的文献的时候.也许网格序列水印这个课题本身的研究意义就是有待考证的.尽管如此,还是要努力的思考下去.由于实验室的原因,iOS的学习进度明显受到影响,加之整理文档本身是一件耗费时间和精力的事情,因此才这么久没有写笔记了. M

【资源】IOS学习资料 - 逆天整理 - 精华无密版【最新】【精华】

 入门看视频,提高看书籍,飘升做项目.老练研开源,高手读外文,大牛讲低调~  01.IOS基础 01.iOS开发快速入门教程 http://pan.baidu.com/s/1kT3ScOf 链接: http://pan.baidu.com/s/1kTKheAF 密码: yycm 02.苹果开发零基础入门教程 http://pan.baidu.com/s/1dDfHL77 链接: http://pan.baidu.com/s/1o6iNkIu 密码: nn3a 03.黑马IOS2期基础 http: