[Hướng dn] Xây dng ng dng vi TabBar và TableView trên iPhone
Trong bài viết này s hướng dn cách s dng TabBar và TableView trong các ng dng trên iPhone. Để to ng
dng, thc hin các bước như sau:
Bước 1: M Xcode, to mt project mi kiu Windows Base và chn kiu là “TabBarWithTableView”.
Bước 2: Xcode s t động to cu trúc thư mc và thêm các framework cn thiết vào project.
Bước 3: Thêm vào project 2 lp kiu UIViewController và đặt tên ln lượt là “TableView” và “SecondView”. Để thêm
lp mi, thc hin như sau: Nhp phi chut vào project New file Cocoa Touch ViewController.
Bước 4: M tp tin TabBarWithTableViewAppDelegate.h và thay đổi các thông tin như sau:
#import <UIKit/UIKit.h>
@interface TabBarWithTableViewAppDelegate : NSObject <UIApplicationDelegate> {
UITabBarController *tabBarControlller;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarControlller;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
Bước 5: Nhp đôi chut vào tp tin MainWindow.xib để m nó bng Interface Builder.
Chèn “TabBar” t thư vin vào ca MainWindow. Chnh li v trí ca TabBar nm cui ca s.
Chn tab đầu tiên ca TabBarController, chn “TableView” mc Identity Inspector. Thc hin các thao tác
tương t vi tab còn li ca TabBarController và chn “SecondView”. Lưu nhng thay đổi trên Interface
Builder và quy li ca s làm vic ca Xcode.
Bước 6: M tp tin TabBarWithTableViewAppDelegate.m và thay đổi như sau:
#import "TabBarWithTableViewAppDelegate.h"
@implementation TabBarWithTableViewAppDelegate
@synthesize window=_window,tabBarControlller;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[_window addSubview:tabBarControlller.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
L p trình iPhone – http://laptrinhdidong.vnPage 1
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
@end
Bước 7: M tp tin TableView.h và thay đổi như sau:
#import <UIKit/UIKit.h>
@interface TableView : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
@property(nonatomic,retain) NSArray *listData;
@end
Bước 8: Nhp đôi chut trái vào tp tin TableView.xib.
Chèn Navigation Bar t thư vin vào ca s Interface Builder.
Chn Navigation Bar trên ca s Interface Builder để m ca s Attribute. Đặt tên cho tiêu đề (Title) cho
Navigation Bar là “TableView”
Kéo “TableView” t thư vin vào th vào ca s Navigation Bar. Chn TableView Connection và chn liên
kết t dataSource đế “File’s Owner”. Lưu nhng thay đổi trên Interface Builder.
Bước 9: M tp tin TableView.m và thay đổi như sau:
#import "TableView.h"
@implementation TableView
@synthesize listData;
// Implement viewDidLoad to do additional setup after loading the view, typically from a
nib.
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc]initWithObjects:@"Monday",@"Tuesday",@"Wednesday",@"T
hursday",@"Friday",@"Saturday",@"Sunday",nil];
self.listData = array;
[array release];
L p trình iPhone – http://laptrinhdidong.vnPage 2
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[listData release];
[super dealloc];
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier]autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
@end
Bước 10: Nhp đôi chut trái tp tin SecondView.xib. Chn view để m ca s Attribute và thay đổi màu nn ca
View. Lưu tp tin SecondView.xib và tr li ca s xCode.
Bước 11: Bây gi bn có th Build ng dng và chy th trên Simulator.
Mi ý kiến đóng góp vui lòng gi vào din đàn. http://laptrinhdidong.vn
Chúc các bn thành công.
L p trình iPhone – http://laptrinhdidong.vnPage 3