上一个例子实现了UINavigationController的简单导航功能,现在结合UITableView把这个功能进一步加强。

AppDelegate.m

//
//  AppDelegate.m
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import "AppDelegate.h"
#import "LoginViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];// 创建LoginViewControllerLoginViewController* loginViewController = [[LoginViewController alloc] init];// 创建UINavigationController,rootViewController设置为loginViewControllerUINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];// 设置window的rootViewController为navigationControllerself.window.rootViewController = navigationController;[self.window makeKeyAndVisible];return YES;
}
@end

LoginViewController.h

//
//  LoginViewController.h
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import <UIKit/UIKit.h>@interface LoginViewController : UIViewController@end

LoginViewController.m

//
//  LoginViewController.m
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import "LoginViewController.h"
#import "MainViewController.h"@interface LoginViewController ()@end@implementation LoginViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.// 设置背景颜色为绿色,用于区分视图self.view.backgroundColor = [UIColor greenColor];// 设置导航栏的titleself.navigationItem.title = @"登陆游戏";// 创建UIBarButtonItem对象UIBarButtonItem* mainBar = [[UIBarButtonItem alloc] initWithTitle:@"英雄列表" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];// 将UIBarButtonItem放在导航栏的右边self.navigationItem.rightBarButtonItem = mainBar;UIImageView* image =  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"lol.jpg"]];image.frame = CGRectMake(0, 100, self.view.frame.size.width, 300);[self.view addSubview:image];}- (void) onClick: (id) sender{// 将MainViewController视图推进堆栈队列[self.navigationController pushViewController:[[MainViewController alloc] init] animated:YES];
}@end

MainViewController.h

//
//  MainViewController.h
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import <UIKit/UIKit.h>@interface MainViewController : UITableViewController@end

MainViewController.m

//
//  MainViewController.m
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import "MainViewController.h"
#import "DetailViewController.h"@interface MainViewController ()@end@implementation MainViewController// 定义英雄集合
NSArray* heroList;- (void)viewDidLoad {[super viewDidLoad];// 设置背景颜色为红色,用于区分视图self.view.backgroundColor = [UIColor redColor];// 设置导航栏的titleself.navigationItem.title = @" 英雄列表";// 初始化英雄集合heroList = @[@"李青",@"瑞文",@"提莫",@"卡兹克",@"卡利斯塔",@"墨菲特",@"泰隆",@"劫",@"杰斯",@"孙悟空",@"阿狸",@"EZ",@"卢锡安",@"卡塔琳娜",@"蔚",@"凯特琳"];// 注册UITableViewCell[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];}#pragma mark - Table view data source// 返回一共有几个分区(sections)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.// Return the number of sections.return 1;
}// 返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.// Return the number of rows in the section.return [heroList count];
}// tableView中每一行显示的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// 获取UITableViewCellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];// 设置每一行的值cell.textLabel.text = [heroList objectAtIndex:indexPath.row];return cell;
}// 当选择每一行时触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{// 创建DetailViewController视图DetailViewController* detailViewController = [[DetailViewController alloc] init];// 给DetailViewController视图传递参数detailViewController.name = [heroList objectAtIndex:indexPath.row];// 将DetailViewController视图推进堆栈队列[self.navigationController pushViewController:detailViewController animated:YES];
}/*// Override to support conditional editing of the table view.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the specified item to be editable.return YES;}*//*// Override to support editing the table view.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {// Delete the row from the data source[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];} else if (editingStyle == UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view}}*//*// Override to support rearranging the table view.- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {}*//*// Override to support conditional rearranging of the table view.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the item to be re-orderable.return YES;}*//*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.}*/@end

DetailViewController.h

//
//  DetailViewController.h
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController// 定义属性,视图跳转时接收参数,用来传递数据
@property (nonatomic,retain) NSString* name;@end

DetailViewController.m

//
//  DetailViewController.m
//  NavigationForwardApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//#import "DetailViewController.h"@interface DetailViewController ()@end@implementation DetailViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.// 设置背景颜色为黄色,用于区分视图self.view.backgroundColor = [UIColor yellowColor];// 设置导航栏的titleself.navigationItem.title = @"英雄信息";// 创建UILabel显示传递过来的数据信息UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];label.text = self.name;[self.view addSubview:label];// 创建UIButton,点击时返回上一个视图UIButton* backButton = [UIButton buttonWithType:UIButtonTypeSystem];backButton.frame = CGRectMake(50, 200, 100, 100);[backButton setTitle:@"返回" forState:UIControlStateNormal];[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];[backButton addTarget:self action:@selector(onBack:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:backButton];}-(void) onBack:(id) sender{// pop出堆栈队列,返回上一个视图[self.navigationController popViewControllerAnimated:YES];
}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.}*/@end

效果图如下:

iOS开发之高级视图—— UINavigationController(二)简单导航功能进阶相关推荐

  1. iOS开发之高级视图—— UITableView(四)自定义Cell

    当我们使用UITableView的时候,经常需要自定义Cell,这个例子展示了一个简单的自定义的Cell. HeroViewCell.h // // HeroViewCell.h // ExtendC ...

  2. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  3. iOS开发中屏幕旋转(二)

    Morris_ 2019.04.11 前面有总结过一些在开发中遇到的屏幕旋转的基础知识. 一.设置应用支持的转屏方向 设置方式 00x1 在TARGET->General->Deploym ...

  4. IOS开发-TableView表视图基础

    表视图在IOS中的应用非常广泛,常用于展示显示数据列表. 在工具组中包含了Table View 和Table View Cell 每个表示图都是UITableView的一个实例,每个可见行都是UITa ...

  5. IOS开发-TableView表视图LV2

    在上一章节IOS开发-TableView表视图基础的学习后, 我觉得对于表视图的学习不应只局限于基础知识的学习,应用在实战中的话想要构建丰富的多元化视图界面我想还是必须深入地再学习下. 于是有了这个L ...

  6. iOS开发UI高级—26Quartz2D使用(信纸条纹)

    iOS开发UI篇-Quartz2D使用(信纸条纹) 一.前导程序 新建一个项目,在主控制器文件中实现以下几行代码,就能轻松的完成图片在视图中的平铺. 1 #import "YYViewCon ...

  7. iOS核心动画高级技术(十二) 性能调优

    Code should run as fast as necessary, but no faster. 代码应该运行的尽量快,而不是更快 - 理查德 在第一和第二部分,我们了解了Core Anima ...

  8. 【iOS开发每日小笔记(二)】gitHub上的开源“瀑布流”使用心得

    这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己 ...

  9. iOS开发如何生成标准的二维码图片

    iOS开发生成标准的二维码图片 废话不多说,下面直接上代码 //使用CIFilter滤镜类生成二维码 - (UIImage *)generateQRCodeWithString:(NSString * ...

最新文章

  1. @Service注解的使用
  2. 文献管理软件 JabRef 快速入门
  3. linux 可执行文件权限不够,root执行/media可执行文件权限不够,chmod修改权限无效...
  4. java k均值_算法——K均值聚類算法(Java實現)
  5. swiper实现局部内容滚动效果
  6. 强烈推荐!孩子的科普从这套全球畅销250万册的最酷科学书起步
  7. 使用LiteOS Studio图形化查看LiteOS在STM32上运行的奥秘
  8. app底部导航栏的设计模板素材
  9. 接口测试Fiddler实战
  10. html去掉右侧滚动条,html中去掉textarea右侧滚动条和右下角拖拽
  11. SoapUI接口测试-基本操作
  12. cmd命令查询电脑序列号_如何在Windows10中查找计算机序列号/主板型号
  13. 能上QQ微信,打不开网页
  14. 如何让新建网站被搜索引擎快速收录
  15. Stm32F4XX开启FPU浮点运算
  16. canvas学习日记一
  17. python爬取3万+条评论,解读猫眼评分9.5的《海王》是否值得一看?
  18. Mac 上的一些骚操作和技巧
  19. R语言及RStudio下载安装
  20. python培训视频课程学习路径

热门文章

  1. Axialis发布了 免费版 的IconWorkshop Lite
  2. c语言二叉树的遍历菜单系统,二叉树遍历C语言的实现
  3. 代写python期末作业价格_代写program留学生作业、代做Python程序语言作业、代写Python课程设计作业...
  4. datediff(datediff函数用于计算两个日期之间的时间)
  5. uc保存html,uctxt 怎么一点就打开?另存为也没用
  6. Monte Calro Tree Search (MCTS)
  7. 安装webpack 报错解决方法
  8. 计算机的优势和劣势_计算机二级证书的含金量不高?别小瞧,这4大优势用处不小...
  9. 如何在内网搭建SFTP服务器,并发布到公网可访问
  10. js部分换行报错的问题解析