一般来说,我们使用的很多app都是使用导航的.这也是我要详细讲解此节的原因.好了,废话不多说,来,走起~
按照学习资料,我要建立一个empty app.然后在里面添加 一个 父类是 UITableViewController 的不含xib 的控制器BIDFirstLevelController.并且,把此控制器作为rootController 哦.
step1:建立根控制器.
产生的文件分别为:
BIDFirstLevelController.h
BIDFirstLevelController.m文件.(操作我就不啰嗦了~)
step2:设置 BIDFirstLevelController 作为我们的root控制器.
在哪里设置呢 ? 必须的需要在 BIDAppDaelegate 文件中设置啊.
i: 在.h文件中建立导航属性:我们想要的导航控制器UINavigationController啦.
#import <UIKit/UIKit.h>
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)UINavigationController *navigationController;
@end
ii:在.m文件中 :把BIDFirstLevelController 和 navigationController 结合在一起,作为我们的rootController吧.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BIDFirstLevelController *first = [[BIDFirstLevelController alloc]initWithStyle:UITableViewStylePlain];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:first];
[self.window addSubview:navigationController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
有没有发现有什么不对的地方?
BIDFirstLevelController *first = [[BIDFirstLevelController alloc]initWithStyle:UITableViewStylePlain];
是么? 这个BIDFirstLevelController 之所以没有xib文件,是因为他最后的属性 UITableViewStylePlain 决定的.这个属性的意思是说:我只需 一列,并且没有分组.所以,这么好的属性是不是就省了很多的操作呢 ?
还有不懂的可能就是在猜测:最终加载成功的navigation 到底是什么样子的呢? 说实话,其实就是在 navigation的下半部分贴了这个first.我们更换视图的时候,只是更换了first,navigation 并没有移除哦.
step3:根视图也创建好了,那么rootcontroller上面的内容在哪里呢 ?好吧,其实吧,我们建立了一个模板,让这个根视图上面的所有视图都依赖这个模板.换句话说:就是给那些子控制器都找同一个父类.这个父类就是 BiDSecondLevelCOntroller.
下面是 BIDSecondLevelContrller的代码:
#import <UIKit/UIKit.h>
@interface BIDSecondViewController : UITableViewController
@property(strong,nonatomic)UIImage *rowImage;
@end
.h文件,我们会发现我的BIDSecondViewController是继承 UITableViewController的.也就是说,这个first控制器上面的所有子控制器都是属于 UITableViewController的.//
// BIDSecondViewController.m
// 第九章:导航控制器的表视图
//
// Created by lichan on 13-11-23.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "BIDSecondViewController.h"
@interface BIDSecondViewController ()
@end
@implementation BIDSecondViewController
@synthesize rowImage;
@end
为什么这个second的代码那么少呢?因为定义的越多,我们的子控制器的限制就越多嘛.
step4:是时候制作我们的子视图了.我们的第一个子视图是:展示按钮视图:
展示按钮视图:大致是说,我们点击一个标签,就会跳转到下一个按钮,就这样,你想设置几个都ok.
我创建的 第一个展示视图名字是:
BIDDisclosureButtonController
.他显示的就是:一个有数据的单表视图和一些有链接视图的ios图标:
@interface BIDDisclosureButtonController : BIDSecondViewController
@property (nonatomic,strong)NSArray *list;
@end
这是我们的数据源的声明.
下面是数据填充:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"Toy Story",
@"A Bug's Life", @"Toy Story 2", @"Monsters, Inc.",
@"Finding Nemo", @"The Incredibles", @"Cars",
@"Ratatouille", @"WALL-E", @"Up", @"Toy Story 3",
@"Cars 2", @"Brave", nil];
self.list = array;
// Do any additional setup after loading the view.
}
下面是基本的tableView的 基本协议函数的实现
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * DisclosureButtonCellIdentifier =
@"DisclosureButtonCellIdentifier";
下面代码是触摸cell和点击图标的两种不同的响应:
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
@"Hey, do you see the disclosure button?"
message:@"If you're trying to drill down, touch that instead"
delegate:nil
cancelButtonTitle:@"Won't happen again"
otherButtonTitles:nil];
[alert show];
}
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
if (childController == nil) {
childController = [[BIDDisclosureDetailController alloc]
initWithNibName:@"BIDDisclosureDetailController" bundle:nil];
}
childController.title = @"Disclosure Button Pressed";
NSUInteger row = [indexPath row];
NSString *selectedMovie = [list objectAtIndex:row];
NSString *detailMessage = [[NSString alloc]
initWithFormat:@"You pressed the disclosure button for %@.",
selectedMovie];
childController.message = detailMessage;
childController.title = selectedMovie;
[self.navigationController pushViewController:childController
animated:YES];
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: DisclosureButtonCellIdentifier];
if (cell == nil)
{ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:
DisclosureButtonCellIdentifier];
}
NSUInteger row = [indexPath row];
NSString *rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;}
我们点击图标发生了什么呢 ?
#import "BIDDisclosureDetailController.h"
@interface BIDDisclosureDetailController ()
@end
@implementation BIDDisclosureDetailController
@synthesize label,message;
-(void)viewWillAppear:(BOOL)animated
{
label.text = message;
[super viewWillAppear:animated];
整体看来,appdelegate-> BIDFirstLevelController->BIDDisclosureCOntroller->BIDDisclosureCOntroller .
是不是很像 app运行顺序呢?