2011年7月2日 星期六

iOS development: Ch6-1 MutiViewController Switches


這次要實作這個是要切換上面圖案中的兩個View~
實作上會多出 BlueView. xib 與 YellowView. xib  及其ViewController,還有下面那條Switch控制列
首先要修改委派 Ch6_1View_SwitcherAppDelegate.h

//修改 委派 Delegate 
#import

@class SwitchViewController;//宣告 SwitchViewController 類別

@interface Ch6_1_View_SwitcherAppDelegate : NSObject {
    UIWindow *window;
SwitchViewController *switchViewController; //建立一個SwitchViewController
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SwitchViewController *switchViewController; //成立IBOutlet
@end
要在MainWindow增加Switch View Controller還有Ch6_1 View Switcher App Delegate

實作Ch6_1View_SwitcherAppDelegate.m
#import "Ch6_1_View_SwitcherAppDelegate.h"
#import "SwitchViewController.h"

@implementation Ch6_1_View_SwitcherAppDelegate

@synthesize window;
@synthesize switchViewController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
[self.window addSubview:switchViewController.view]; //將根控制器的view加入視窗
    [self.window makeKeyAndVisible];
    
    return YES;
}
=========================================================
//  SwitchViewController.h
//  Ch6-1 View Switcher
//
//  Created by jason on 2011/6/30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import
@class YellowViewController;
@class BlueViewController;

@interface SwitchViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}

@property (nonatomic , retain) YellowViewController *yellowViewController;
@property (nonatomic , retain) BlueViewController *blueViewController;
- (IBAction) switchViews: (id) sender;

@end
=========================================================
建立控制列

SwitchViewController.m
這時候也會加入blueView到View的子檢視

- (void)viewDidLoad {//覆寫載入nib檔的方法
//建立BlueViewController,使用 initWithNibName: 方法 ,參數為 .xib檔名(不帶附檔名)  
BlueViewController *blueController=[[BlueViewController alloc] initWithNibName: @"BlueView" bundle: nil];
self.blueViewController=blueController;//將建好的ViewController指派給blueViewController
[self.view insertSubview:blueController.view atIndex:0]; // BlueView 加到根檢視(view)底下當子檢視 , index設為 0 表示將其放到所有東西的背後確保我們加入的工具列可以顯示出來
[blueController release];
[super viewDidLoad];
}
需要時才會將yellow view加入,同時也會放掉blueview

-(IBAction) switchViews: (id) sender{ //我們在switchViews,才將yellowViewControll載入以確保不會照成記憶體的負擔

if(self.yellowViewController.view.superview ==nil){// 若目前檢視不是yellow,那就更換成如下檢視
if(self.yellowViewController ==nil){//先確定是否沒有yellowViewController才開始如下建立它
YellowViewController *yellowController=[[YellowViewController alloc] initWithNibName: @"YellowView" bundle: nil];
self.yellowViewController=yellowController;
[yellowController release];
[blueViewController.view removeFromSuperview]; //將blueView移除
[self.view insertSubview:yellowViewController.view atIndex:0]; //加入根檢視
}else{ //否則(若目前檢視不是blue,那就更換成如下檢視)
if(self.blueViewController ==nil){ //確定是沒有blueViewController,則如下建立它
BlueViewController *blueController=[[BlueViewController alloc] initWithNibName: @"BlueView" bundle: nil];
self.blueViewController=blueController;
[blueController release];
}
[yellowViewController.view removeFromSuperview];//yellowView移除
[self.view insertSubview:blueViewController.view atIndex:0];//加入根檢視
}
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
//在沒有使用到該檢視控制器時就將其記憶體空間釋放
if(self.blueViewController.view.superview==nil)
self.blueViewController=nil;
else 
self.yellowViewController =nil;
}


- (void)dealloc {
[yellowViewController release];
[blueViewController release];
    [super dealloc];
}
========================================================= 
增加警告標示

//  YellowViewController.h
//  Ch6-1 View Switcher
//
//  Created by jason on 2011/7/2.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import


@interface YellowViewController : UIViewController {

}
-(IBAction) yellowButtonPressed;
@end
========================================================================
//  YellowViewController.m
//  Ch6-1 View Switcher
//
//  Created by jason on 2011/7/2.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "YellowViewController.h"

@implementation YellowViewController

-(IBAction) yellowButtonPressed{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"yellow View Button Pressed" 
message: @"You pressed the button on the yellow view" 
  delegate:nil
  cancelButtonTitle:@"Yep, It works"
  otherButtonTitles:nil];
[alert show];
[alert release];
}

沒有留言: