导航控制器和表视图(一)
2012-07-13 20:54
330 查看
这两天在看导航控制器和表视图这一块,发现很苦逼,特别是XIB文件关联的时候,要不是关联不上,拉着没反应,要不是关联上了,结果苦逼的错了,造成内存泄露等问题,导致失败。但是这些失败也不是浪费时间的,从中还是能获得经验的;因为前面练习的时候一直坚持用代码编写,但是想着苹果公司开发出这么好的开发工具,如果不会去用,那岂不是更苦逼了,现在用的是有问题,但都是时间问题,如果能理解XIB的关联操作一定分享下心的。哎哎,也不抱怨了,开始介绍今天内容;
实现功能是:表视图每一行都有拓展图标,显示该行的详细内容。表视图上面是一个导航栏,导航栏上面有返回上一级按钮,然后就是来回的切换;先展示下所实现的效果图:
![](http://img.my.csdn.net/uploads/201304/21/1366513865_7909.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513869_5303.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513874_7810.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513879_9140.png)
再说一下导航栏控制器,UINavigationController,主要用于分层的应用程序开发,管理视图的进入和退出,主要是通过栈来实现,如果不知道栈是什么建议还是在网上先了解一下栈的原理,此处用栈的内容不是太多;
实现步骤
1.新建工程名为NavigationController , File->New->Project ->single View Application -> next
2.在新建一个文件控制器FirstLevelViewController,不生成XIB文件,继承UITableViewController
3.设置导航控制器
在NavViewContoller.h中声明一个导航控制器输出口,在NavViewContoller.h中添加@synthesize navigationController = _navigationController;
![](http://img.my.csdn.net/uploads/201304/21/1366514025_1475.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514030_5514.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514040_2536.png)
5.关联输出口,选中navigationController
![](http://img.my.csdn.net/uploads/201304/21/1366514045_6396.png)
6.单击View Controller-RootViewcontroller,将底层类改为FirstLevelViewController,这样,在加载nib文件时候,当行控制器将于FirstLevelViewController一起实例化;NIB Name属性保持为空,不做更改
![](http://img.my.csdn.net/uploads/201304/21/1366514166_7655.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514172_9502.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514177_5000.png)
7.从开始文章截图上我们看到,第一级视图上,FirstLevel下面表视图单元格上左边有一个Image属性,我们可以创建一个UITableViewController子类,因为UITableViewController有UIImage属性,可以显示图片,而不是用UIImage属性直接添加每个子控制器中,然后子类化这个新类;
创建二级视图控制器,SecondLevelController,Subclass of 一栏选UITableViewController,然后添加UIImage属性
在.m文件中添加 @synthesize rowImage = _rowImage;
8.实现FirstLevelViewController,
代理方法
还需要添加头文件 #import "SecondLevelViewController.h" 以上完成应该是这样的
![](http://img.my.csdn.net/uploads/201304/21/1366514322_6822.png)
9.在新建一个控制器,DIsclosureViewController和DisclosureDetailViewController,第二个勾选生成XIB
![](http://img.my.csdn.net/uploads/201304/21/1366514237_3876.png)
11.为DIsclosureViewControoler填充代码
附上代码:http://download.csdn.net/detail/duxinfeng2010/4428970
实现功能是:表视图每一行都有拓展图标,显示该行的详细内容。表视图上面是一个导航栏,导航栏上面有返回上一级按钮,然后就是来回的切换;先展示下所实现的效果图:
![](http://img.my.csdn.net/uploads/201304/21/1366513865_7909.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513869_5303.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513874_7810.png)
![](http://img.my.csdn.net/uploads/201304/21/1366513879_9140.png)
再说一下导航栏控制器,UINavigationController,主要用于分层的应用程序开发,管理视图的进入和退出,主要是通过栈来实现,如果不知道栈是什么建议还是在网上先了解一下栈的原理,此处用栈的内容不是太多;
实现步骤
1.新建工程名为NavigationController , File->New->Project ->single View Application -> next
2.在新建一个文件控制器FirstLevelViewController,不生成XIB文件,继承UITableViewController
3.设置导航控制器
在NavViewContoller.h中声明一个导航控制器输出口,在NavViewContoller.h中添加@synthesize navigationController = _navigationController;
#import <UIKit/UIKit.h> @interface NavViewController : UIViewController @property(strong,nonatomic) IBOutlet UINavigationController *navigationController; @end4.创建导航控制器,连接输出口navigationController,打开NavViewController.xib文件,拖动一个NavigationController,自动生成如图所示导航视图
![](http://img.my.csdn.net/uploads/201304/21/1366514025_1475.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514030_5514.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514040_2536.png)
5.关联输出口,选中navigationController
![](http://img.my.csdn.net/uploads/201304/21/1366514045_6396.png)
6.单击View Controller-RootViewcontroller,将底层类改为FirstLevelViewController,这样,在加载nib文件时候,当行控制器将于FirstLevelViewController一起实例化;NIB Name属性保持为空,不做更改
![](http://img.my.csdn.net/uploads/201304/21/1366514166_7655.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514172_9502.png)
![](http://img.my.csdn.net/uploads/201304/21/1366514177_5000.png)
7.从开始文章截图上我们看到,第一级视图上,FirstLevel下面表视图单元格上左边有一个Image属性,我们可以创建一个UITableViewController子类,因为UITableViewController有UIImage属性,可以显示图片,而不是用UIImage属性直接添加每个子控制器中,然后子类化这个新类;
创建二级视图控制器,SecondLevelController,Subclass of 一栏选UITableViewController,然后添加UIImage属性
#import <UIKit/UIKit.h> @interface SecondLevelViewController : UITableViewController @property (strong,nonatomic) UIImage *rowImage; @end
在.m文件中添加 @synthesize rowImage = _rowImage;
8.实现FirstLevelViewController,
#import <UIKit/UIKit.h> @interface FirstLevelViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> @property(strong,nonatomic) NSArray *controllers; @end
- (void)viewDidLoad { [super viewDidLoad]; UITableView *tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416) style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; [self.view addSubview:tableView]; // 设置导航控制器标题,让用户知道当前所处位置 self.title = @"First Level"; NSMutableArray *array = [[NSMutableArray alloc]init]; self.controllers = array; }
代理方法
#pragma mark - #pragma mark Table Data Soursen Methods -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.controllers count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *FirstLevelCell = @"FirstLevelCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell]; if (cell == nil) { cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell]; } NSUInteger row = [indexPath row]; SecondLevelViewController *controller = [self.controllers objectAtIndex:row]; cell.textLabel.text = controller.title; cell.imageView.image = controller.rowImage; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } #pragma mark - #pragma mark Table View Delegate Methods -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; SecondLevelViewController *nextController = [self.controllers objectAtIndex:row]; [self.navigationController pushViewController:nextController animated:YES]; }
还需要添加头文件 #import "SecondLevelViewController.h" 以上完成应该是这样的
![](http://img.my.csdn.net/uploads/201304/21/1366514322_6822.png)
9.在新建一个控制器,DIsclosureViewController和DisclosureDetailViewController,第二个勾选生成XIB
![](http://img.my.csdn.net/uploads/201304/21/1366514237_3876.png)
#import <UIKit/UIKit.h> @interface DisclosureDetailViewController : UIViewController @property (strong,nonatomic) IBOutlet UILabel *label; @property (nonatomic,copy) NSString *message; @end 在DisclosureDetailViewController.xib文件拖一个UILabel,用于显示文字,进行关联 #import "DisclosureDetailViewController.h" @interface DisclosureDetailViewController () @end
@implementation DisclosureDetailViewController @synthesize label = _label; @synthesize message =_message; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [super viewDidUnload]; self.label=nil; self.message = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(void) viewWillAppear:(BOOL)animated { self.label.text = self.message; [super viewWillAppear:animated]; } @end
11.为DIsclosureViewControoler填充代码
#import <UIKit/UIKit.h> #import "SecondLevelViewController.h" #import "DisclosureDetailViewController.h" @interface DIsclosureButtonController :SecondLevelViewController<UITableViewDataSource,UITableViewDelegate> @property (strong,nonatomic) NSArray *list; @property (strong,nonatomic) DisclosureDetailViewController * childController; @end
#import "DIsclosureButtonController.h" #import "NavAppDelegate.h" #import "DisclosureDetailViewController.h" @implementation DIsclosureButtonController @synthesize list =_list; @synthesize childController=_childController; -(void)viewDidLoad { NSArray *array = [[NSArray alloc]initWithObjects:@"Toy Sooory",@"A Bug life",@"Toy Sooory2",@"Monsters,Inc",@"FInderDemo",@"The Incredibles",@"Cars",@"Ratatouille",@"WALL-E",@"Up",@"Toy SOOOry3",@"Brave",@"Down",@"cCao", nil]; self.list =array; [super viewDidLoad]; } -(void) viewDidUnload { self.list=nil; self.childController=nil; } #pragma mark - #pragma mark Table Data Souce Methods -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.list count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *DisClosureButtonCellIdentfier= @"DisClosureButtonCellIdentier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DisClosureButtonCellIdentfier]; if (cell == nil) { cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DisClosureButtonCellIdentfier]; } NSUInteger row = [indexPath row]; NSString *rowString = [self.list objectAtIndex:row]; cell.textLabel.text = rowString; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; return cell; } #pragma mark - #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 are trying to drill down,touch that instaed" delegate:nil cancelButtonTitle:@"wont't happed again" otherButtonTitles: nil]; [alert show]; } -(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { if (self.childController == nil) { self.childController = [[DisclosureDetailViewController alloc]initWithNibName:@"DisclosureDetailViewController" bundle:nil]; } self.childController.title =@"DisClosure Button Pressed"; NSUInteger row = [indexPath row]; NSString *selectedMovie = [self.list objectAtIndex:row]; NSString *detailMessage = [[NSString alloc]initWithFormat:@"you pressed the disclosure button for %@",selectedMovie]; self.childController.message = detailMessage; self.childController.title = selectedMovie; [self.navigationController pushViewController:self.childController animated:YES]; } @end
附上代码:http://download.csdn.net/detail/duxinfeng2010/4428970
相关文章推荐
- Iphone开发用导航控制器实现视图的分层切换(UINavigationController)
- 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值
- UI07_导航视图控制器-从后往前传值
- 7 高级UI控件-导航控制器与表视图
- xcode6如何新建一个空项目+添加导航视图控制器
- iOS在自定义视图(常用于自定义cell)中获取导航控制器上面的当前的控制器对象
- Objective--C 导航视图控制器
- tabbarcontroller 内嵌导航 控制器,2层push hide tabbar 后 ,第二层直接返回根视图控制器选择tabbarcontroller的其它vc 无法显示 tabbar的 问题解决方案
- UI 07 _ 导航视图控制器 与 属性传值
- iOS 导航控制器和表视图
- ipad UISplitViewController 导航视图控制器标题设置
- ABC定制视图导航控制器
- 导航控制器和表视图(二)
- IOS5基础之十四-----导航控制器和表视图--创建顶级视图
- iOS 导航控制器、标签、表视图的创建方法
- [IOS]添加NavigationItem到导航视图控制器
- Snail—UI学习之导航视图控制器UINavigationController(系统)
- 导航视图控制器(总结)
- Iphone开发(十三)用导航控制器实现视图的分层切换(UINavigationController)
- 导航控制器和表视图(二)