导航条或者说导航栏目现在在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,如需转载请自行联系原作者