导航器和表视图

本节内容是学习《iphone4与ipad开发基础教程》第九章的笔记。感觉本章内容比较重要,做了一下记录。

UINavigationController是用于构建分成应用程序的主要工具,他在管理换入,换出多个内容视图方面和UITabBarController比较类似.不同之处在于UINavifationController是作为stack实现的,这让它非常适合处理分层数据。

关于栈的几个基本定义在这里不介绍了,主要有栈的特性,入栈,出栈,基栈,栈顶等概念。

UINavigationController维护一个视图控制器栈,任何类型的视图控制器都会被加入栈中,在设计导航控制器时,需要指定用户看到的第一个视图,该视图是视图层次结构中最底层的视图。其控制器称为root view controller。当用户查看下一个视图时,栈中将加入一个新的视图控制器,该视图控制器管理的视图将展现给用户,称之为subController。

在下面的Nav例子中,程序包含一个导航控制器和6子控制器。该例子包含的内容有,导航控制器的使用,表视图的使用。

解释:该例子没有使用Xcode中提供的模板。

1,创建一个Window-Based Application项目。

2,创建导航层次关系的第一级视图,FirstLevelController类。该类继承NSObject。在代码中对FirstLevelController做如下更改。

FirstLevelController.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface FirstLevelViewController : UITableViewController {
	NSArray *controllers;
}
@property (nonatomic,retain)NSArray *controllers;
@end

FirstLevelController.m

#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclusureButtonController.h"
#import "CheckListController.h"
#import "RowControlsController.h"
#import "MoveMeController.h"
#import "DeleteMeController.h"
#import "PresidentsViewController.h"

@implementation FirstLevelViewController
@synthesize controllers;

-(void) viewDidLoad{
	//设置当前活动的控制器的title
	self.title = @"First level";
	NSMutableArray *array = [[NSMutableArray alloc] init];
	
	//Disclosure Button
	DisclusureButtonController *disclusureButtonController = [[DisclusureButtonController alloc] initWithStyle:UITableViewStylePlain];
	disclusureButtonController.title = @"Disclosure Button";
	disclusureButtonController.rowImage = [UIImage imageNamed:@"disclosureButtonControllerIcon.png"];
	[array addObject:disclusureButtonController];
	[disclusureButtonController release];
	
	// Check List 
    CheckListController *checkListController = [[CheckListController alloc]
                                                initWithStyle:UITableViewStylePlain];
    checkListController.title = @"Check One";
    checkListController.rowImage = [UIImage imageNamed:
								@"checkmarkControllerIcon.png"];
    [array addObject:checkListController];
    [checkListController release];
	
	
	// Table Row Controls
    RowControlsController *rowControlsController = [[RowControlsController alloc] initWithStyle:UITableViewStylePlain];
    rowControlsController.title = @"Row Controls";
    rowControlsController.rowImage = [UIImage imageNamed:@"rowControlsIcon.png"];
    [array addObject:rowControlsController];
    [rowControlsController release];
	
	
	// Move Me
    MoveMeController *moveMeController = [[MoveMeController alloc]
                                          initWithStyle:UITableViewStylePlain];
    moveMeController.title = @"Move Me";
    moveMeController.rowImage = [UIImage imageNamed:@"moveMeIcon.png"];
    [array addObject:moveMeController];
    [moveMeController release];
	
	
	// Delete Me
    DeleteMeController *deleteMeController = [[DeleteMeController alloc] 
                                              initWithStyle:UITableViewStylePlain];
    deleteMeController.title = @"Delete Me";
    deleteMeController.rowImage = [UIImage imageNamed:@"deleteMeIcon.png"];
    [array addObject:deleteMeController];
    [deleteMeController release];
	
	
	// President View/Edit
    PresidentsViewController *presidentsViewController = 
    [[PresidentsViewController alloc] 
     initWithStyle:UITableViewStylePlain];
    presidentsViewController.title = @"Detail Edit";
    presidentsViewController.rowImage = [UIImage imageNamed:
                                         @"detailEditIcon.png"];
    [array addObject:presidentsViewController];
    [presidentsViewController release];  
	
	self.controllers = array;
	[array release];
	[super viewDidLoad];
}

-(void)viewDidUnload{
	self.controllers = nil;
	[super viewDidUnload];
}

-(void) dealloc{
	[controllers release];
	[super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Method

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [self.controllers count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	NSString *identifier = @"FirstLevelCell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									   reuseIdentifier:identifier] autorelease];
	}
	
	//configure the cell
	NSInteger 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 Tableview Delegate Methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	NSInteger row = [indexPath row];
	SecondLevelViewController *nextController = [self.controllers objectAtIndex:row];
	[self.navigationController pushViewController:nextController
										 animated:YES];
}
@end
FirstLevelViewController.h和FirstLevelViewController.m文件包含用户看到的第一个表视图控制器。但是呢,其实他并不是应用程序的根控制器。应用程序的根控制器是苹果公司提供的导航控制器,它不需要子类化。
3,设置导航控制器。编辑应用程序委托,将导航控制器的视图添加到应用程序窗口。
NavAppDelegate.h
#import <UIKit/UIKit.h>

@interface MyNavAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	UINavigationController *navController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
//为导航控制器添加一个输出口navController
@property (nonatomic,retain) IBOutlet UINavigationController *navController; 
@end
NavAppDelegate.m
#import "MyNavAppDelegate.h"

@implementation MyNavAppDelegate

@synthesize window;
@synthesize navController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
	[navController release];
    [window release];
    [super dealloc];
}
@end

接着我们需要做的是:创建一个导航控制器,将它连接到刚才声明的navController输出口,然后告知导航控制器使用什么作为它的根视图控制器。

将导航控制器连接到navController输出口。右键Nav App Delegate图标,拖动到新的Navigation Controller图标上,选择navController输出口。

告知导航控制器用什么作为他的根视图选择器。

4,创建SecondLevelViewController.h和SecondLevelViewController.m:该类是二级控制器的父类。

SecondLevelViewController.h

#import <Foundation/Foundation.h>


@interface SecondLevelViewController : UITableViewController {
	UIImage *rowImage;
}
@property (nonatomic,retain)UIImage *rowImage;

@end

SecondLevelViewController.m

#import "SecondLevelViewController.h"


@implementation SecondLevelViewController
@synthesize rowImage;
@end

到此为止我们的应用程序框架基本上已经搭建起来了,下面需要做的就是实现具体的二级控制器。并将其添加到一级控制器的表视图中。

(未完待续)  

下面天剑第一个子控制器(展示按钮视图)  

  

  

  

转载于:https://www.cnblogs.com/lucaszyl/archive/2012/05/02/iphone%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值