上一个例子实现了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];
// 创建LoginViewController
LoginViewController* loginViewController = [[LoginViewController alloc] init];
// 创建UINavigationController,rootViewController设置为loginViewController
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
// 设置window的rootViewController为navigationController
self.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];
// 设置导航栏的title
self.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];
// 设置导航栏的title
self.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 {
// 获取UITableViewCell
UITableViewCell *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];
// 设置导航栏的title
self.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
效果图如下: