Tải bản đầy đủ (.docx) (4 trang)

Xây dựng ứng dụng với TabBar và TableView trên iPhone pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (93.05 KB, 4 trang )

Trung tâm Tin học – ĐH KHTN
[Hướng dẫn] Xây dựng ứng dụng với TabBar và TableView trên iPhone
Trong bài viết này sẽ hướng dẫn cách sử dụng TabBar và TableView trong các ứng dụng trên iPhone. Để tạo ứng
dụng, thực hiện các bước như sau:
Bước 1: Mở Xcode, tạo một project mới kiểu Windows Base và chọn kiểu là “TabBarWithTableView”.
Bước 2: Xcode sẽ tự động tạo cấu trúc thư mục và thêm các framework cần thiết vào project.
Bước 3: Thêm vào project 2 lớp kiểu UIViewController và đặt tên lần lượt là “TableView” và “SecondView”. Để thêm
lớp mới, thực hiện như sau: Nhấp phải chuột vào project  New file  Cocoa Touch  ViewController.
Bước 4: Mở tập 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: Nhấp đôi chuột vào tập tin MainWindow.xib để mở nó bằng Interface Builder.
• Chèn “TabBar” từ thư viện vào cửa MainWindow. Chỉnh lại vị trí của TabBar nằm cuối cửa sổ.
• Chọn tab đầu tiên của TabBarController, chọn “TableView” ở mục Identity Inspector. Thực hiện các thao tác
tương tự với tab còn lại của TabBarController và chọn “SecondView”. Lưu những thay đổi trên Interface
Builder và quy lại cửa sổ làm việc của Xcode.
Bước 6: Mở tập 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
{
}
Lập trình iPhone – age 1
Trung tâm Tin học – ĐH KHTN
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
@end
Bước 7: Mở tập 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: Nhấp đôi chuột trái vào tập tin TableView.xib.
• Chèn Navigation Bar từ thư viện vào của sổ Interface Builder.
• Chọn Navigation Bar trên của sổ Interface Builder để mở của sổ Attribute. Đặt tên cho tiêu đề (Title) cho
Navigation Bar là “TableView”
• Kéo “TableView” từ thư viện vào thả vào của sổ Navigation Bar. Chọn TableView  Connection và chọn liên
kết từ dataSource đế “File’s Owner”. Lưu những thay đổi trên Interface Builder.
Bước 9: Mở tập 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 {

Lập trình iPhone – age 2
Trung tâm Tin học – ĐH KHTN
NSArray *array = [[NSArray alloc]initWithObjects:@"Monday",@"Tuesday",@"Wednesday",@
"Thursday",@"Friday",@"Saturday",@"Sunday",nil];

self.listData = array;
[array release];
[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;
Lập trình iPhone – age 3
Trung tâm Tin học – ĐH KHTN

}
@end
Bước 10: Nhấp đôi chuột trái tập tin SecondView.xib. Chọn view để mở của sổ Attribute và thay đổi màu nền của
View. Lưu tập tin SecondView.xib và trở lại cửa sổ xCode.
Bước 11: Bây giờ bạn có thể Build ứng dụng và chạy thử trên Simulator.
Mọi ý kiến đóng góp vui lòng gởi vào diễn đàn.
Chúc các bạn thành công.
Lập trình iPhone – age 4

×