iOS开发-UINavigationController简单介绍

十度 IOS 2015年12月01日 收藏

导航条或者说导航栏目现在在App中基本上也算是标配,类似于父子级别的味道在里面,UINavigationController就是负责简化这一实现功能的,属于iOS开发中比较常用的一种容器View controller,很多人都用,实现起来相对比较容易,可以先看张图片了解NavigationController:

界面布局

上面看着很高大上,下面看个个人的,从控件库中拖入一个Navigation Controller,然后新建两个ViewController:

 

拖入的一个NavigationController可以直接使用,需要演示,后面有多加了一个View:

Demo实现

主页的ViewController中初始化一下数据:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4. data=[[NSArray alloc] initWithObjects:@"前端",@"后端",nil];
  5. [self.tableView setDelegate:self];
  6. [self.tableView setDataSource:self];
  7. }

 设置组数,行数和内容:

  1. }
  2. //分组的数量
  3. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  4. return 1;
  5. }
  6.  
  7.  
  8. //分组的行数
  9. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  10. return [data count];
  11. }
  12. //返回TableCell中内容
  13. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  14. UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  15. [cell.textLabel setText:data[indexPath.row]];
  16.  
  17. return cell;
  18. }

 设置行高:

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  2. return 40;
  3. }

 设置跳转事件:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2. NSArray *notificationData=[[NSArray alloc]init];
  3. NSInteger index=[self.tableView indexPathForSelectedRow].row;
  4. if (index==0) {
  5. notificationData=[[NSArray alloc]initWithObjects:@"Android",@"iOS",@"JS",nil];
  6. }else{
  7. notificationData=[[NSArray alloc]initWithObjects:@"Java",@"C#",@"Python",nil];
  8. }
  9. SecondViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondStoryID"];
  10. controller.data=notificationData;
  11. [self.navigationController pushViewController:controller animated:YES];
  12. }

 详情的ViewController和第一个类似,之前已经写过很多TableView的实现,直接贴代码,就不一一解释:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4.  
  5. }
  6. - (void)notificationHandler:(NSNotification *)notification{
  7. self.data=[notification object];
  8. }
  9. //分组的数量
  10. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  11. return 1;
  12. }
  13.  
  14.  
  15. //分组的行数
  16. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  17. return [self.data count];
  18. }
  19. //返回TableCell中内容
  20. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  21. UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  22. [cell.textLabel setText:self.data[indexPath.row]];
  23. NSLog(@"%@",self.data[indexPath.row]);
  24. return cell;
  25. }
  26. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  27. return 40;
  28. }

不过详情的头文件中要设置一下:

  1. @interface SecondViewController : UIViewController
  2.  
  3. @property (nonatomic,strong) NSArray *data;
  4.  
  5. @end

最终实现的效果如下:

 

绿色背景的导航条需要个人设置: