2011年4月27日 星期三

view switcher(三)-SwitchViewController








//

//  SwitchViewController.h

#import <UIKit/UIKit.h>

@class BlueViewController;
@class YellowViewController;

@interface SwitchViewController : UIViewController {
BlueViewController *_blueViewController;
YellowViewController *_yellowViewController;

}

@property (nonatomic, retain) BlueViewController *_blueViewController;
@property (nonatomic, retain) YellowViewController *_yellowViewController;

-(IBAction)switchViews:(id)sender;

@end

修改MainWindow.xib
按兩下MainWindow.xib開啓IB, 必須再建立用來表示root controller的實例; 因為IB程式庫沒有SwitchViewController, 所以必須加入一個view controller,再將其類別改為SwitchViewController
(圖一)將UIViewController拉到nib時所產生的view



接下來建立root controller的view, root controller的view將包含螢幕下方的一個工具列
之前我們將UIViewController拉到nib主視窗時, 產生了一個新視窗(如第一張圖), 要在此視窗為root controller(SwitchViewController)建立view

SwitchViewController主要任務是切換BlueView與YellowView, 所以必須有可以讓使用者改變檢視的方法, 所以在工具列增加一按鈕讓使用者切換檢視

從Library拖曳View到(圖一), Library拖曳Toolbar
更改button name 為 Switch Views

接著將動作與button連接
選擇MainWindow.xib的Switch View Controller, 選擇Inspector的Connections Inspector, 將switchViews方法連結至Toolbar的button (如下圖)

之前在SwitchViewControllerAppDelegate.h建立IBOutlet (_switchViewController), 因此應用程式可以連接到SwitchViewController的實例, 並在主要的應用程式視窗中加入sub view,..  所以必須在nib中將SwitchViewController實例連接到此IBOutlet
選擇Switch View Controller App Delegate, 選擇Inspector的Connections Inspector,將_switchViewController連接到SwitchViewController的實例, 如下圖

儲存nib, 開始編輯SwitchViewController.m

//  SwitchViewController.m
//  SwitchViewController

//

#import "SwitchViewController.h"

#import "BlueViewController.h"
#import "YellowViewController.h"

@implementation SwitchViewController

@synthesize _blueViewController;
@synthesize _yellowViewController;

-(IBAction)switchViews:(id)sender
{
//先檢查_yellowViewController的view之上層view是否為nil
if (self._yellowViewController.view.superview == nil
{
//檢察_yellowViewController是否被建立, 若尚未被建立, 則下列if成立 
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)viewDidLoad {
BlueViewController *blueController = [[BlueViewController alloc
  initWithNibName:@"BlueView" bundle:nil];
self._blueViewController = blueController;
//將藍色檢視加入成為根檢視的子檢視, 索引值為0表示此子檢視放在所有東西的最下層,
//如此可確保工具列永遠都看的見
[self.view insertSubview:blueController atIndex:0];
[blueController release];
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
if (self._blueViewController.view.superview == nil
{
self._blueViewController = nil;
}
else 
{
self._yellowViewController = nil;
}

}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end

將兩個檢視加入按鈕觸發動作

//
//  BlueViewController.h

#import <UIKit/UIKit.h>

@interface BlueViewController : UIViewController {

}

-(IBAction)blueButtonPressed;

@end


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

@interface YellowViewController : UIViewController {

}

-(IBAction)yellowButtonPressed;

@end

將兩個檢視與 xib連結,  分別開啓BlueView.xib , YellowView.xib
ex: double click "BlueView.xib", select File's Owner, 按下command+4呼叫Identity Inspector
NSObject 改為 BlueViewController

更改view背景顏色為藍色

增加button, 連接blueButtonPressed, 除了連接IBAction, 還要將BlueViewController的View接口連接到nib中的view

YellowView的做法也與BlueView相同








沒有留言:

張貼留言