IOS5基礎之五-----多視圖應用程序

創建一個ViewSwitcher,在模板選擇時選擇Empty Application。

接下來要創建視圖控制器和nib文件,如圖:


選中左邊的View Switch command+n 出現右邊的圖,選擇UIViewControllersubClass然後Next。


然後輸入類名稱。

Subclass of 視圖佈局和表格佈局。

Taregeed for IPad  創建IPad。

with XIB for user interface 同時創建一個與此控制器相關聯的nib文件。

同理可以創建BIDBlueViewController和BIDYellowController。


創建GDI




這裏注意的是要Group中選中View Switch。

同理創建BlueView.xib 和Yellow.xib

修改應用程序頭文件中

#import<UIKit/UIKit.h>

@classBIDSwitchViewController;

@interface BIDAppDelegate :UIResponder <UIApplicationDelegate>

@property (strong,nonatomic) UIWindow *window;

@property (strong,nonatomic)BIDSwitchViewController *switchViewController;//指向應用程序的根控制器

@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

   // Override point for customization after application launch.

    

   self.switchViewController=[[BIDSwitchViewControlleralloc] initWithNibName:@"SwitchView"bundle:nil];

   UIView *switchView=self.switchViewController.view;

    CGRect switchViewFrame=switchView.frame;

    switchViewFrame.origin.y+=[UIApplicationsharedApplication].statusBarFrame.size.height;

    switchView.frame=switchViewFrame;

    [self.windowaddSubview:switchView];

    

   self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

   return YES;

}


修改BIDSwitchViewController.h

#import<UIKit/UIKit.h>

@classBIDYellowViewController;

@classBIDBlueViewController;


@interface BIDSwitchViewController :UIViewController

@property (strong,nonatomic)BIDYellowViewController *yellowViewController;

@property (strong ,nonatomic)BIDBlueViewController *blueViewController;

-(IBAction)switchViews:(id)sender;

@end


添加視圖控制器關聯
Class 原來的NSObjet 修改爲如圖所示。
Received Action會變成switchViews。

選擇視圖,在視圖中拖入ToolBar並且修改按鈕值爲Switch View.


將Switch Views按鈕關聯到File's Owner,並且選擇switchViews操作。File‘s Owner重新關聯View,選擇view。

beginAnimations:Context:接受兩個參數 第一個是動畫塊標題,第二個是指針與此動畫塊關聯的對象。

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];設置動畫曲線

[UIView setAnimationTransition

UIViewAnimationTransitionFlipFromLeft 設置轉換類型iOS提供四種裝換類型

UIViewAnimationTransitionFlipFromRight

UIViewAnimationTransitionCurlUp

UIViewAnimationTransitionCurlDown                             


#import"BIDSwitchViewController.h"

#import"BIDBlueViewController.h"

#import"BIDYellowViewController.h"

@implementation BIDSwitchViewController;

@synthesize yellowViewController;

@synthesize blueViewController;


- (void)viewDidLoad

{

   self.blueViewController = [[BIDBlueViewControlleralloc]

                              initWithNibName:@"BlueView"bundle:nil];//這裏覆蓋了將在載入nib時調用的UIViewController方法。

    [self.viewinsertSubview:self.blueViewController.viewatIndex:0];

    [superviewDidLoad];

}


- (IBAction)switchViews:(id)sender {

    [UIViewbeginAnimations:@"View Flip"context:nil];

    [UIViewsetAnimationDuration:1.25];

    [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

    

   if (self.yellowViewController.view.superview == nil) {

       if (self.yellowViewController ==nil) {

           self.yellowViewController =

            [[BIDYellowViewControlleralloc] initWithNibName:@"YellowView"

                                                     bundle:nil];

        }

        [UIViewsetAnimationTransition:

        UIViewAnimationTransitionFlipFromRight

                              forView:self.viewcache:YES];

        

        [self.blueViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.yellowViewController.viewatIndex:0];

    } else {

        if (self.blueViewController ==nil) {

           self.blueViewController =

            [[BIDBlueViewControlleralloc] initWithNibName:@"BlueView"

                                                   bundle:nil];

        }

        [UIViewsetAnimationTransition:

        UIViewAnimationTransitionFlipFromLeft

                              forView:self.viewcache:YES];

        

        [self.yellowViewController.viewremoveFromSuperview];

        [self.viewinsertSubview:self.blueViewController.viewatIndex:0];

    }

    [UIViewcommitAnimations];

}


- (void)didReceiveMemoryWarning

{

   // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];  

   // Release any cached data, images, etc that aren't in use.

    

   if (self.blueViewController.view.superview == nil) {

       self.blueViewController =nil;

    } else {

       self.yellowViewController =nil;

    }

}


實現內容視圖
BIDBlueViewController的頭文件增加

-(IBAction)blueButtonPressed;

打開BlueView.xib 把File’s Owner的class改爲BIDBlueViewController。在屬性檢查器中修改background爲藍色。


sattus Bar修改爲None Botton Bar 修改爲ToolBar。
在頁面上添加一個按鈕


找到按鈕的連接檢查器,選擇Touch Up Inside 事件關聯到File‘s Owner上連接blueButtonPressed方法。

這個方法也就是添加一個警告。

-(IBAction)blueButtonPressed

{

   UIAlertView *alert=[[UIAlertViewalloc] initWithTitle:@"Blue View Button Pressed"message:@"You pressed the button on the blue view"delegate:nilcancelButtonTitle:@"Yes,I did."otherButtonTitles:nil];

    [alert show];

}

上次根據Xcode3.2.5中的操作,很多地方和4.2的相差很多,再加上對xcode,不熟悉,導致這個到現在纔出來,真是很慚愧。尷尬,繼續努力學習!搞了一天,鬱悶啊!



發佈了102 篇原創文章 · 獲贊 35 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章