導覽列或者說導航欄目現在在App中基本上也算是标配,類似于父子級别的味道在裡面,UINavigationController就是負責簡化這一實作功能的,屬于iOS開發中比較常用的一種容器View controller,很多人都用,實作起來相對比較容易,可以先看張圖檔了解NavigationController:

上面看着很高大上,下面看個個人的,從控件庫中拖入一個Navigation Controller,然後建立兩個ViewController:
拖入的一個NavigationController可以直接使用,需要示範,後面有多加了一個View:
首頁的ViewController中初始化一下資料:
1
2
3
4
5
6
7
<code>- (</code><code>void</code><code>)viewDidLoad {</code>
<code> </code><code>[</code><code>super</code> <code>viewDidLoad];</code>
<code> </code><code>// Do any additional setup after loading the view, typically from a nib.</code>
<code> </code><code>data=[[</code><code>NSArray</code> <code>alloc] initWithObjects:@</code><code>"前端"</code><code>,@</code><code>"後端"</code><code>,</code><code>nil</code><code>];</code>
<code> </code><code>[</code><code>self</code><code>.tableView setDelegate:</code><code>self</code><code>];</code>
<code> </code><code>[</code><code>self</code><code>.tableView setDataSource:</code><code>self</code><code>];</code>
<code>}</code>
設定組數,行數和内容:
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>//分組的數量</code>
<code>- (</code><code>NSInteger</code><code>)numberOfSectionsInTableView:(UITableView *)tableView{</code>
<code> </code><code>return</code> <code>1;</code>
<code>//分組的行數</code>
<code>- (</code><code>NSInteger</code><code>)tableView:(UITableView *)tableView numberOfRowsInSection:(</code><code>NSInteger</code><code>)section{</code>
<code> </code><code>return</code> <code>[data count];</code>
<code>//傳回TableCell中内容</code>
<code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(</code><code>NSIndexPath</code> <code>*)indexPath{</code>
<code> </code>
<code> </code><code>UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:</code><code>nil</code><code>];</code>
<code> </code><code>[cell.textLabel setText:data[indexPath.row]];</code>
<code> </code><code>return</code> <code>cell;</code>
設定行高:
<code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(</code><code>NSIndexPath</code> <code>*)indexPath{</code>
<code> </code><code>return</code> <code>40;</code>
設定跳轉事件:
<code>- (</code><code>void</code><code>)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(</code><code>NSIndexPath</code> <code>*)indexPath{</code>
<code> </code><code>NSArray</code> <code>*notificationData=[[</code><code>NSArray</code> <code>alloc]init];</code>
<code> </code><code>NSInteger</code> <code>index=[</code><code>self</code><code>.tableView indexPathForSelectedRow].row;</code>
<code> </code><code>if</code> <code>(index==0) {</code>
<code> </code><code>notificationData=[[</code><code>NSArray</code> <code>alloc]initWithObjects:@</code><code>"Android"</code><code>,@</code><code>"iOS"</code><code>,@</code><code>"JS"</code><code>,</code><code>nil</code><code>];</code>
<code> </code><code>}</code><code>else</code><code>{</code>
<code> </code><code>notificationData=[[</code><code>NSArray</code> <code>alloc]initWithObjects:@</code><code>"Java"</code><code>,@</code><code>"C#"</code><code>,@</code><code>"Python"</code><code>,</code><code>nil</code><code>];</code>
<code> </code><code>}</code>
<code> </code><code>SecondViewController *controller = [</code><code>self</code><code>.storyboard instantiateViewControllerWithIdentifier:@</code><code>"SecondStoryID"</code><code>];</code>
<code> </code><code>controller.data=notificationData;</code>
<code> </code><code>[</code><code>self</code><code>.navigationController pushViewController:controller animated:</code><code>YES</code><code>];</code>
詳情的ViewController和第一個類似,之前已經寫過很多TableView的實作,直接貼代碼,就不一一解釋:
21
22
23
24
25
26
27
28
29
<code>- (</code><code>void</code><code>)notificationHandler:(</code><code>NSNotification</code> <code>*)notification{</code>
<code> </code><code>self</code><code>.data=[notification object];</code>
<code> </code><code>return</code> <code>[</code><code>self</code><code>.data count];</code>
<code> </code><code>[cell.textLabel setText:</code><code>self</code><code>.data[indexPath.row]];</code>
<code> </code><code>NSLog</code><code>(@</code><code>"%@"</code><code>,</code><code>self</code><code>.data[indexPath.row]);</code>
不過詳情的頭檔案中要設定一下:
<code>@interface</code> <code>SecondViewController : UIViewController</code>
<code>@property</code> <code>(</code><code>nonatomic</code><code>,strong) </code><code>NSArray</code> <code>*data;</code>
<code>@end</code>
最終實作的效果如下:
綠色背景的導覽列需要個人設定:
本文轉自Fly_Elephant部落格園部落格,原文連結:http://www.cnblogs.com/xiaofeixiang/p/4251837.html,如需轉載請自行聯系原作者