IOS編程多視圖應用程式完成後心得體會
使用Xcode的開發平台相對於使用c++ builder的開發平台來講有幾點不同:
1、Xcode建立一個window-base工程同CB中建立一個Forms Application工程相似,都會建立一個類class,作為主表單,都自動產生3個檔案;
2、Xcode建立的新表單,是未與代碼中的類連結,必須在身份檢查器中修改File’s Owner的class預設為NSObject,將其改為代碼中定義了的類,例如,BlueViewController。
例如:
雙擊BlueView.xib,單擊File’s Owner表徵圖,並按下⌘4開啟身份檢查器。File’s Owner的class預設為NSObject,將其改為BlueViewController。
圖6-14 修改身份檢查器
3、在Xcode編寫代碼比CB中編寫代碼更加方便,因為編寫時會自動搜尋相關連的代碼,並顯示出來供操作員選擇,因此一個小小的應用其代碼都很多,很長。
我學會了如何編寫一個多視圖的應用:
步驟:
1、建立工程
2、建立視圖控制器和nib檔案
(1)單擊Groups & Files窗格中的Classes檔案夾,然後按下⌘N或從File菜單中選擇New File…從左側邊窗格中選擇Cocoa Touch Classes,可以看到用於常用的Cocoa Touch類的模板。選擇UIViewController subclass。在右側邊窗格中,可以看到一個標有With XIB for user interface的複選框。如果該複選框處於被選中狀態,則單擊它以取消選擇。
單擊Next,輸入名稱SwitchViewController.m確保選中了Also create “SwitchViewController.h”,然後單擊Finish。
(2)建立兩個nib檔案,分別對應剛才建立的兩個內容視圖。單擊Group&Files窗格中的Resource檔案夾,以便在正確的位置建立它們。按下⌘N或從File菜單中選擇New File…,在左側邊窗格中的iPhone OS標題下選擇User Interface ,選擇View XIB模板的表徵圖,這將建立一個帶有內容視圖的nib檔案,然後單擊Next按鈕,輸入BlueView.xib。
1. 修改應用程式委託
(1)單擊Groups&Files窗格中的View_SwitchAppDelegate.h檔案,並對檔案進行修改:
#import <UIKit/UIKit.h>
@class SwitchViewController;
@interface View_SwitcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) SwitchViewController *switchViewController;
@end
(2)修改View_SwitherAppDelegate.m添加代碼將根控制器的視圖添加到應用程式的主視窗:
#import “View_SwitherAppDelegate.h”
#import “SwitchViewController.h”
@synthesize window;
@synthesize switchViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:switchViewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[switchViewController release];
[super dealloc];
}
2. 修改SwitchViewController.h
#import <UIKit/UIKit.h>
@class BlueViewController;
@class YellowViewController;
@interface SwitchViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property (retain, nonatomic) YellowViewController *yellowViewController;
@property (retain, nonatomic) BlueViewController *blueViewController;
-(IBAction)switchViews:(id)sender;
@end
3. 修改MainWindow.xib檔案
(1)雙擊MainWindow.xib,在Interface Builder中開啟它。從Library中添加一個視圖控制器(View Controller)
圖6-10 添加視圖控制器
(2)在nib的主視窗中單擊View Controller表徵圖,並按下⌘4開啟身份檢查器:將其類修改為SwitchViewController
圖6-11 身份檢查器
(3)從庫中拖一個View到SwitchViewController對應的視窗中。
圖6-12 添加視圖
(4)從庫中拖動一個工具列到視圖上,將其放在底部。雙擊該工具列的按鈕將其標題改為Switch View,按住Ctrl鍵將該按鈕拖到Switch View Controller表徵圖,然後選擇switchViews:操作。
圖6-13 添加工具條
(5)按住Ctrl鍵並從View_SwicherAppDelegate表徵圖拖到SwitchViewController表徵圖,然後選擇swicthViewController輸出口。
4. 修改SwitchViewController.m
#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;
- (void)viewDidLoad
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@”BlueView” bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
}
- (IBAction)switchViews:(id)sender
{
if(self.yellowViewController.view.superview == nil)
{
if(self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:@”YellowView” bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
else if(self.blueViewController == nil)
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@”BlueView” bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if (self.blueViewController.view.superview == nil)
self.blueViewController = nil;
else
self.yellowViewController = nil;
}
- (void)dealloc {
[yellowViewController release];
[blueViewController release];
[super dealloc];
}
5. 實現內容視圖
(1)修改blueView內容視圖控制器的聲明檔案:
#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController {
}
-(IBAction)blueButtonPressed:(id)sender;
@end
(2)修改yellowView視圖控制器的聲明檔案:
#import <UIKit/UIKit.h>
@interface YellowViewController : UIViewController {
}
- (IBAction)yellowButtonPressed:(id)sender;
@end
(3)雙擊BlueView.xib,單擊File’s Owner表徵圖,並按下⌘4開啟身份檢查器。File’s Owner的class預設為NSObject,將其改為BlueViewController。
圖6-14 修改身份檢查器
(4)單擊View表徵圖,然後⌘1調出屬性偵測器。單擊標有BackGround的顏色,將此視圖的背景顏色設定為藍色。
圖6-15 修改視圖背景顏色
(5)從庫中拖出一個Round Rect Button到視窗上。雙擊該按鈕,將其標題改為Press Me,將該按鈕放到合適的地方。然後切換到串連檢查器,將Touch Up Inside事件拖到File’s Owner表徵圖,選擇blueButtonPressed:操作方法。
圖6-16 串連操作方法
(6)將BlueViewController的view輸出口串連到nib中的視圖。
圖6-17 串連視圖輸出口
(7)雙擊YellowView.xib檔案,對此Nib檔案做同樣的修改,將按鈕標題修改為Press me,Too,背景顏色設定為黃色。
(8)開啟BlueViewController.m檔案實現操作方法
- (IBAction)blueButtonPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Blue View Button Pressed”
message:@”You pressed the button on the blue view”
delegate:nil
cancelButtonTitle:@”Yep, I did.”
otherButtonTitles:nil];
[alert show];
[alert release];
}
(9)開啟YellowViewController.m檔案實現操作方法
-(IBAction)yellowButtonPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Yellow View Button Pressed”
message:@”You pressed the button on the yellow view”
delegate:nil
cancelButtonTitle:@”Yep, I did.”
otherButtonTitles:nil];
[alert show];
[alert release];
}
6. 製作轉換動作
(1)修改switchViews:操作方法。
- (IBAction)switchViews:(id)sender
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.yellowViewController.view.superview == nil)
{
if (self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc] initWithNibName:@"YellowView"
bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
}
else
{
if (self.blueViewController == nil)
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@"BlueView"
bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[yellowViewController viewWillAppear:YES];
[blueViewController viewWillDisappear:YES];
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
[blueViewController viewDidDisappear:YES];
[yellowViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}
7. 運行程式,點擊Press Me按鈕。
圖6-18 運行效果
8. 點擊Switch View按鈕,然後點擊Press Me ,Too按鈕。