接上节:
1.我们用TabBarApplication项目,在Interface Builder中,我们从Livrary拖拉Tab Bar项目到Tab Bar窗口中,拉到最下面,这样就多了一个项目.
2.点击选中我们新添加的这个Tab Bar项目,在Attributes属性窗口,我们设置它的Badge值为5,Identifier属性为Search.看到项目的图标变成了一个放大镜.
3.回到Xcode,右键点击Classes,我们选择增加一个New File.选择cocoa Touch Classes,选择UIViewContoller子类.命名为SearchViewContoller.m(这里要注意要把With xib for user interface选中)
4.可以看到我们多了几个文件,我们双击我们新建立的SearchView.xib文件,添加一个Label到里面
5,我们回到MainWindow.xib,点击Tab Bar,选中我们新添加的项目,在它的属性窗口中我们设置NIB名字为SearchviewController.

6.Search Tab Bar项目的属性窗口中,设置类为SearchViewController.
7,保存,运行.一定要是在Xcode里面运行.
这个原理就比较简单了.不多说了.Charpter4本章完结.3,4章为学习Xcode的重点中的重点,一切的基础都在这里面,大家多多练习,多多实践.为后面的章节打好基础.
有同学要习题,这里有一些,大家试试看吧
1.用代码来创建一个窗口控制程序.
2.用代码来实现动态窗口.
3.用代码来实现一个事件,比如弹出.选择等等
4.多自己写几个动态警告窗口程序.
目前我们已经讲过了3种应用程序的开发:Viewbased Application,Windowbased Application,和Split Viewbased Application.还有一种比较常用的应用程序就是我们这节讲学习的Tab Bar Application.
创建一个Tab Bar Application
1.Xcode,创建一个项目,命名为TabBarApplication
2.查看项目内容,我们可以看到里面和前面所举例的项目中有所不同.里面包含了2个View控制器,(FirstViewController和SecondViewController)和3个XIB文件(MainWindow.xib,FirestView.xib和SecondView.xib).如图
3.查看TabBarApplicationAppDelegate.h文件
#import <UIKit/UIKit.h>
@interface TabBarApplicationAppDelegate : NSObject
<UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
我们用UITableViewcontroller类取代了以往用的UIViewController类.功能一样,不过更具体一些.
4.当应用程序完成加载时,UITableViewcontroller实例的当前窗口被读取.这个在TabBarApplicationAppDelegate.m文件中实现
#import “TabBarApplicationAppDelegate.h”
@implementation TabBarApplicationAppDelegate
@synthesize window;
@synthesize tabBarController;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 在当前控制器窗口添加一个tab子窗口
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
5.双击MainWindow.xib文件,可以看到我们已经有了一个包含2个Tab Bar项目的窗口.
6.点击第一个Tab Bar(被标签First).在Identify Inspector窗口,我们可以看到实现的类为FirstViewController.
我们在Attributes Inspector 窗口可以看到,它与FirstView.xib文件进行了链接
7.点击第二个Tab Bar项目,在Attributes和Identify属性窗口我们可以看到和第一个项目类似的设置,只不过链接的类和xib文件变为了SeconView.
8.回到Xcode,我们先运行看看基础程序是什么样的效果.点击看大图
原理:
这一切都是用UITableViewcontroller类来实现的.双击MainWindow.xib文件,我们可以看到多了一个Tab Bar Controller项目.
Tab Bar控制器应该说是View控制器的一个集合,由两个view控制器组成.当程序加载后UITabBarViewcontroller将会一直被显示,你点击屏幕下方的2个选项,点哪个,就显示哪个.
当然我们也可以添加更多的tab选项,显示更多的页面!
前面我们已经做好了一个切分窗口,具体怎么应用我们通过下面的例子来实现,比如我们在左边列出一些电影的名字,当我们点击这些电影的时候,右边主窗口显示出相应的电影海报图片.
例子:Displaying Some Items
1.继续沿用splitViewBasedApp项目,双击DetailView.xib文件进行编辑
2.在View窗口添加一个图片应用窗口,在Attributes Inspector窗口中设置它的模式为Aspect Fit,如图
3.点击刚才添加的view窗口,在Size Inspector窗口中对Autosizing 进行设定
➤➤ X: 152
➤➤ Y: 163
➤➤ W: 463
➤➤ H: 644
4.我们添加一些电影海报在我们的Resources文件夹内,大家可以下载这里的源程序包,里面有图片和源码,也可以自己去找几个图片来代替,注意大小就是了.
5.在DetailViewcontroller.h文件中,我们需要添加红色代码:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
<UIPopoverControllerDelegate, UISplitViewControllerDelegate> {
UIPopoverController *popoverController;
UINavigationBar *navigationBar;
id detailItem;
IBOutlet UIImageView *imageView;
}
@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;
@property (nonatomic, retain) id detailItem;
@property (nonatomic, retain) UIImageView *imageView;
@end
6.做好链接,按住Ctrl点击拖拽File's Owner项目到图片窗口,选择imgageView
7.在RootViewController.m文件中添加红色代码:
#import “RootViewController.h”
#import “DetailViewController.h”
@implementation RootViewController
@synthesize detailViewController;
NSMutableArray *listOfMovies;
- (void)viewDidLoad {
//---初始化一个数组,后面的图片名称可以根据你的需求自己修改---
listOfMovies = [[NSMutableArray alloc] init];
[listOfMovies addObject:@“Training Day”];
[listOfMovies addObject:@“Remember the Titans”];
[listOfMovies addObject:@“John Q.”];
[listOfMovies addObject:@“The Bone Collector”];
[listOfMovies addObject:@“Ricochet”];
[listOfMovies addObject:@“The Siege”];
[listOfMovies addObject:@“Malcolm X”];
[listOfMovies addObject:@“Antwone Fisher”];
[listOfMovies addObject:@“Courage Under Fire”];
[listOfMovies addObject:@“He Got Game”];
[listOfMovies addObject:@“The Pelican Brief”];
[listOfMovies addObject:@“Glory”];
[listOfMovies addObject:@“The Preacher’s Wife”];
//---设置 title---
self.navigationItem.title = @“Movies”;
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
- (NSInteger)tableView:(UITableView *)aTableView
numberOfRowsInSection:(NSInteger)section {
// 返回我们有多少行的数值.
//return 10;
return [listOfMovies count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @“CellIdentifier”;
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell.
//cell.textLabel.text = [NSString stringWithFormat:@“Row %d”, indexPath.row];
cell.textLabel.text = [listOfMovies objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)aTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
When a row is selected, set the detail view controller’s detail item to
the item associated with the selected row.
*/
//detailViewController.detailItem =
// [NSString stringWithFormat:@“Row %d”, indexPath.row];
detailViewController.detailItem = [NSString stringWithFormat:@“%@“,
[listOfMovies objectAtIndex:indexPath.row]];
}
- (void)dealloc {
//施放内存很重要啊,前面不提是因为那些小程序占用内存少,如果是图片和电影那就大了,你不想你的程序用了几个后,机器慢如蜗牛.iPad,iPhone自由256,和512的内存.
[listOfMovies release];
[super dealloc];
}
8.在DetailViewController.m文件中添加如下红色代码:
#import “DetailViewController.h”
#import “RootViewController.h”
@implementation DetailViewController
@synthesize navigationBar, popoverController, detailItem;
@synthesize imageView;
/*
这段代码用来控制气泡窗口的显示和取消.
*/
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
// Update the view.
navigationBar.topItem.title = [detailItem description];
NSString *imageName = [NSString
stringWithFormat:@“%@.jpg”,navigationBar.topItem.title];
imageView.image = [UIImage imageNamed:imageName];
}
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
}
9.运行,看看效果.
有成就感吧,大家!继续努力,我们来了解一下这是怎么实现的
首先看前面那一大段图片定义代码:巴拉巴拉....
- (void)viewDidLoad {
//---initialize the array---
listOfMovies = [[NSMutableArray alloc] init];
[listOfMovies addObject:@“Training Day”];
[listOfMovies addObject:@“Remember the Titans”];
[listOfMovies addObject:@“John Q.”];
[listOfMovies addObject:@“...................不写了,节约版面
.............
我们用tableView:numberOfRowsInSection:方法来返回我们需要显示的行数值,和数组的大小
- (NSInteger)tableView:(UITableView *)aTableView
numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//return 10;
return [listOfMovies count];
}
用tableView:cellForRowAtIndexPath:方法对列表中的每个项目定义或者赋予数值,以便列表窗口的显示:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @“CellIdentifier”;
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell.
//cell.textLabel.text = [NSString stringWithFormat:@“Row %d”, indexPath.row];
cell.textLabel.text = [listOfMovies objectAtIndex:indexPath.row];
return cell;
}
当我们在列表窗口对项目进行选择的时候,我通过DetailViewController给出他们的属性值detailItem
- (void)tableView:(UITableView *)aTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
When a row is selected, set the detail view controller’s
detail item to the item associated with the selected row.
*/
//detailViewController.detailItem =
// [NSString stringWithFormat:@“Row %d”, indexPath.row];
detailViewController.detailItem = [NSString stringWithFormat:@“%@“,
[listOfMovies objectAtIndex:indexPath.row]];
}
在DetailViewController.m文件中,我们定义setDetailItem:方法用来显示我们的每张图片:
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
// Update the view.
navigationBar.topItem.title = [detailItem description];
NSString *imageName = [NSString
stringWithFormat:@“%@.jpg”,navigationBar.topItem.title];
imageView.image = [UIImage imageNamed:imageName];
}
if (popoverController != nil) {
[popoverController dismissPopoverAnimated:YES];
}
}
由此实现我们要实现的东西.
端午节来了,放假了,周末了,无心工作学习啊,为什么假期这么频繁呢?

我是一个超级苹果迷+老乔的fans和Google的簇拥,一切新奇的事物我都好奇,摄影、运动、旅游、读书、电影、游戏都是我的爱好,荒淫大家给我留言,发邮件,请我吃饭、带我旅游(也不要忘记了我的领导同志!)……
最近留言