Navigation Controller建立步驟
来源:互联网 发布:nginx keepalive 配置 编辑:程序博客网 时间:2024/04/29 20:06
Navigation(導覽) Controller是一個可用來導覽內容的方式。以下有兩種方式可以將這個控制器建立到應用程式中,一種是使用initWithNibName的方式,另一種則是利用Interface Builder協助載入view的方式。兩種方式事實上都是很常見,但有許多程式設計師,會認為第一種會比較有操控感,少用Interface Builder可以讓除錯容易,也更容易從程式碼來理解寫了什麼東西。以下的第一種方式,去除了重載initWithNibName的作法,為了和第二種方式來對應。這裡只是展示了,用兩種方式都可以達到同一個目的,在許多的開發教學中,也會常常見到不同的作法。另外,有一種方式是直接使用在新增Project時,就直接選擇Navigation-based Application,到底幫你作了什麼事?下面第二種方式有說明。
一、利用initWithNibName來載入view的方式:
AppDelegate.h
←加入宣告UINavigationController *navigationController;
(並不需要用ib來加nav)
AppDelegate.m
←在applicationDidFinishLaunching區域中加入以下的步驟:
1、加入第一個view controller的header file
#import "FirstViewController.h"
#import "FirstViewController.h"
2、alloc init這個nav controller
navigationController = [[UINavigationController alloc] init];
3、加入這個nav controller到window的subview中(用addSubview)
[window addSubview:navigationController.view];
4、用第一個view的controller建立一個新的view controller,並用 initWithNibName初始化
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
5、用nav controller的pushViewController將第一個view顯現(不需要動畫所以animated設為NO)
[navigationController pushViewController:viewController animated:NO];
6、release剛push完的view controller
[viewController release];
7、在dealloc方法中加入nav controller的release宣告
[navigationController release];
FirstViewControler.h
←加入一個- (IBAction)push:(id)sender;
FirstViewController.m
1、加入第二個view controller的header file
#import "SecondViewController.h"
2、實作push按鈕的action
//2-1初始化
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
//2-2用pushViewController加入,動畫也設為YES
[self.navigationController pushViewController:secondViewController animated:YES];
//2-3把這個加入過的view controller給release
[secondViewController release];
3、在viewDidLoad方法中設title(nav controller用的)
self.title = @"First";
SecondViewController.h
SecondViewController.m
1、在viewDidLoad方法中設title(nav controller用的)
self.title = @"Second";
二、利用Interface Builder輔助載入view的方式
(註:在建立一個新的project,選擇Navigation-based Application是直接完成以下的1~5步驟)
(和上述的方式不同處是不需要管第一個要載入的root view,只要設定好nav controller和app delegate的關係,以及設定好是root view是那一個view controller就會自動載入了)
1、建立新project選擇Windows-based Application。AppDelegate.h中加入一個新的IBOutlet UINavigationController。
UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
2、在Classes新增一個新的檔案,選擇Cocoa Touch Classes→UIViewController Subclass,並勾選Option With XIB for user interface,假設名稱為FirstViewController,完成後把.xib檔移至Resource資料夾中
3、雙點擊MainWindow.xib打開Interface Builder,從Library中拉一個Navigation Controller到主視窗中(有File's Owner那個),點選Navigation Controller,然後點出Connection Inspector將設定如下圖。(按住ctrl從Connection Inspector直接拉往主視窗中的xxx App Delegate然後放開)
4、點選Navigation Controller下的Root View Controller,點出Attributes和Identity Inspector設定如下圖。
5、AppDelegate.m加入以下程式碼
@synthesize navigationController;
,並在applicationDidFinishLaunching方法中加入
[window addSubview:navigationController.view];
dealloc方法中加入nav controller的release宣告
[navigationController release];
此時第一個view會正確先載入
6、依照2的說明再加一個新的view controller,名稱為SecondViewController
7、再點擊MainWindow.xib打開Interface Builder,加入一個view controller在最下方,依照第4點的方法選用SecondViewController
8、FirstViewControler.h
←加入一個- (IBAction)push:(id)sender;
9、FirstViewController.m
加入第二個view controller的header file
#import "SecondViewController.h"
實作push按鈕的action
//初始化
SecondViewController *secondViewController = [[SecondViewController alloc] init];
//用pushViewController加入,動畫也設為YES
[self.navigationController pushViewController:secondViewController animated:YES];
//把這個加入過的view controller給release
[secondViewController release];
在viewDidLoad方法中設title(nav controller用的)
self.title = @"First";
SecondViewController.h
SecondViewController.m
在viewDidLoad方法中設title(nav controller用的)
self.title = @"Second";
- Navigation Controller建立步驟
- storyboard 添加Navigation Controller
- Navigation Controller 学习
- navigation controller学习总结
- navigation返回前面某一个controller
- iOS学习- 05 Navigation Controller
- iOS storyboard创建Navigation Controller
- [ROS]如何建立Navigation Stack
- Navigation 与 tableview controller 一起使用
- 在AppDelegate.m外实现navigation controller
- swift入门之CustomViewController Navigation Controller
- 导航栏视图控制器(navigation controller)
- IOS学习——Navigation Controller
- ios中navigation controller的漂移问题
- Storyboard快速创建带有Navigation的Controller
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (1)
- iOS 5 Storyboard 学习之 Tabbar Controller,Navigation Controller (3)
- 建立VPN十步
- c c++ static之谈
- 通过交换a,b 中的元素,使[序列a 元素的和]与[序列b 元素的和]之间的差最小
- 创新工厂面试题
- MyEclipse 快捷键及经验总结
- 复制链表
- Navigation Controller建立步驟
- Android 知识碎片
- 20111005 用mega128读取遥控接收机信号
- 经典js代码收藏(非本人原创)
- XML初探(二)DTD校验
- 嵌入式linux下挂载nfs出现permission denied的解决方法
- 由信息论的熵引出称球问题问题的一般解法
- 10.5 近几天小结
- arch linux下安装vmware8