导航控制器和表视图(一)

来源:互联网 发布:瑞金麟集团 知乎 编辑:程序博客网 时间:2024/05/17 06:44

        这两天在看导航控制器和表视图这一块,发现很苦逼,特别是XIB文件关联的时候,要不是关联不上,拉着没反应,要不是关联上了,结果苦逼的错了,造成内存泄露等问题,导致失败。但是这些失败也不是浪费时间的,从中还是能获得经验的;因为前面练习的时候一直坚持用代码编写,但是想着苹果公司开发出这么好的开发工具,如果不会去用,那岂不是更苦逼了,现在用的是有问题,但都是时间问题,如果能理解XIB的关联操作一定分享下心的。哎哎,也不抱怨了,开始介绍今天内容;

实现功能是:表视图每一行都有拓展图标,显示该行的详细内容。表视图上面是一个导航栏,导航栏上面有返回上一级按钮,然后就是来回的切换;先展示下所实现的效果图:

      


再说一下导航栏控制器,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;@end
4.创建导航控制器,连接输出口navigationController,打开NavViewController.xib文件,拖动一个NavigationController,自动生成如图所示导航视图

   
5.关联输出口,选中navigationController


6.单击View Controller-RootViewcontroller,将底层类改为FirstLevelViewController,这样,在加载nib文件时候,当行控制器将于FirstLevelViewController一起实例化;NIB Name属性保持为空,不做更改

    

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"  以上完成应该是这样的


9.在新建一个控制器,DIsclosureViewController和DisclosureDetailViewController,第二个勾选生成XIB



#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



原创粉丝点击