国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

從零開始學(xué)ios開發(fā)(十):MultiviewApplications(多個(gè)xib之前的切換)

2019-11-14 20:22:01
字體:
供稿:網(wǎng)友

這篇學(xué)習(xí)的主要內(nèi)容是Multiview,在我們學(xué)習(xí)iphone旋轉(zhuǎn)的時(shí)候,介紹過多個(gè)view的使用方法,不過這里的view和旋轉(zhuǎn)屏幕中所指的多個(gè)view是不同的,旋轉(zhuǎn)屏幕中涉及到的多個(gè)view是在一個(gè)xib文件中的,而我們這里所指的mulitview,則是指多個(gè)xib,在多個(gè)xib中進(jìn)行view的切換,也就是從一個(gè)xib切換到另一個(gè)xib,而每個(gè)xib中只有一個(gè)view。

另外的一個(gè)不同點(diǎn)體現(xiàn)在創(chuàng)建項(xiàng)目的時(shí)候,到目前為止,我們創(chuàng)建的所有項(xiàng)目的template都是single view,這次創(chuàng)建的項(xiàng)目將使用新的template。

Multiview applicatin的基本架構(gòu),一般來說,一個(gè)multiview application會(huì)有一個(gè)主要的controller來控制view的呈現(xiàn),這個(gè)主要的controller可以是toolbar(iphone上的Safari打開后,地下的一排按鈕就是toolbar)或者是tab bar(iphone打開phone,最下面一個(gè)一個(gè)的tab),或者其他的一些控件,他們控制到底那個(gè)view應(yīng)該顯示,那個(gè)view應(yīng)該被隱藏,也就是說,至少需要3個(gè)view才能實(shí)現(xiàn)view的切換,一個(gè)主要的controller view,2個(gè)其他的用于切換的view,主要的controller view是一直顯示在屏幕上的,而其他的view中,只有1個(gè)會(huì)顯示出來,其他的都被隱藏。ok,下面開始一步一步實(shí)現(xiàn)multiview吧,再說下去會(huì)越來越糊涂的。

0)項(xiàng)目簡介 今天要做的例子中包含3個(gè)view,一個(gè)controller view,我們會(huì)使用toolbar,2個(gè)用于切換的view,一個(gè)藍(lán)色底,一個(gè)黃色底,他們中間都有一個(gè)button,單擊button會(huì)有一個(gè)警告框彈出,告訴用戶當(dāng)前顯示的是哪個(gè)view。

1)創(chuàng)建一個(gè)工程,選擇Empty Application 這次不再選擇Single View Application,而選擇Empty Application,項(xiàng)目中的所有文件我們都會(huì)手動(dòng)進(jìn)行添加。

單擊Next按鈕,之后的操作和創(chuàng)建Single View項(xiàng)目一樣,設(shè)定項(xiàng)目名稱“View Switcher”,設(shè)定項(xiàng)目保存路徑,項(xiàng)目創(chuàng)建完成。

2)添加View Controller 由于我們使用的模板是Empty Application,因此當(dāng)創(chuàng)建完項(xiàng)目后,只有以下一些文件 里面并沒有我們需要的controller view,也沒有任何xib文件,這些都是需要我們手動(dòng)添加的。使用快捷鍵command+N或者菜單欄File>New>New File...,在彈出的窗口中,左邊選擇Cocoa Touch,右邊選擇UIViewController subclass,點(diǎn)擊Next按鈕

填寫類名BIDSwitchViewController,其他都是默認(rèn)選項(xiàng)(注意最后一個(gè)checkbox,如果選擇了,則將創(chuàng)建一個(gè)和BIDSwitchViewController關(guān)聯(lián)的xib文件,我們在這里可以選上,但是為了弄清楚view controller和xib文件是如何關(guān)聯(lián)在一起的,在這個(gè)項(xiàng)目中我們暫時(shí)不選,后面我們會(huì)手動(dòng)連接這兩個(gè)文件),點(diǎn)擊Next按鈕。 選擇保持的位置,保存在“View Switcher”目錄下,完成創(chuàng)建。

BIDSwitchViewController是項(xiàng)目的最頂層的view controller(root controller),它用于控制另外2個(gè)view的切換,下面按照同樣的方法,創(chuàng)建另外2個(gè)view controller,一個(gè)名字教BIDBlueViewController,另一個(gè)叫做BIDYellowViewController,他們都不需要關(guān)聯(lián)xib,且都保存在“View Switcher”目錄下。創(chuàng)建完成后的“View Switcher”結(jié)構(gòu)如下

3)添加xib文件 使用快捷鍵command+N或者菜單欄File>New>New File...,在彈出的窗口中,左邊選擇User Interface,右邊選擇View,點(diǎn)擊Next按鈕

Device Family中選擇iphone,點(diǎn)擊Next

命名為SwitchView.xib,同樣保持在“View Switcher”目錄下,點(diǎn)擊Create,完成創(chuàng)建。 使用同樣的方法創(chuàng)建另外兩個(gè)xib,分別命名為BlueView.xib和YellowView.xib。至此,我們所有的文件都已經(jīng)創(chuàng)建完畢,整個(gè)的“View Switcher”結(jié)構(gòu)圖如下 接下來就是寫代碼的工作了。

4)編輯BIDAppDelegate文件 當(dāng)一個(gè)app啟動(dòng)的時(shí)候,我們都會(huì)默認(rèn)的把一個(gè)view載入當(dāng)前的iphone窗口(application's main window),在這個(gè)例子中,這個(gè)view就是我們的root view,即BIDSwitchViewController。我們是在BIDAppDelegate文件中設(shè)置默認(rèn)載入的view的,因此首先打開BIDAppDelegate.h,添加class BIDSwitchViewController,和它的一個(gè)PRoperty,如下

復(fù)制代碼
#import <UIKit/UIKit.h>@class BIDSwitchViewController;@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) BIDSwitchViewController *switchViewController;@end
復(fù)制代碼

其中,@class是告訴BIDAppDelegate,后面的BIDSwitchViewController是一個(gè)類,應(yīng)該以類的方式處理該對象,后面在聲明property的時(shí)候,BIDAppDelegate就知道BIDSwitchViewController是一個(gè)類,不會(huì)不認(rèn)該對象。

接著打開BIDAppDelegate.m,添加如下代碼

復(fù)制代碼
#import "BIDAppDelegate.h"#import "BIDSwitchViewController.h"@implementation BIDAppDelegate@synthesize window = _window;@synthesize switchViewController;- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.        self.switchViewController = [[BIDSwitchViewController alloc] initWithNibName:@"SwitchView" bundle:nil];    UIView *switchView = self.switchViewController.view;    CGRect switchViewFrame = switchView.frame;    switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;    switchView.frame = switchViewFrame;    [self.window addSubview:switchView];        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}......
復(fù)制代碼

首先import BIDSwitchViewController,然后聲明synthesize,對應(yīng)于頭文件中的property。

didFinishLaunchingWithOption方法,當(dāng)一個(gè)app載入完成后需要做一些什么事情,在這里,我們指定了哪個(gè)view被載入到windows中作為默認(rèn)顯示的view。

self.switchViewController = [[BIDSwitchViewControlleralloc] initWithNibName:@"SwitchView" bundle:nil]; 通過xib(在舊的版本中,xib被稱為nib,因此這里出現(xiàn)的是NibName)的名字來制定初始化哪個(gè)view

UIView *switchView = self.switchViewController.view; 獲取view

CGRect switchViewFrame = switchView.frame; 得到view的frame,也就是這個(gè)view的顯示位置,在前幾篇的文章中提到過這個(gè)屬性。

switchViewFrame.origin.y += [UIApplicationsharedApplication].statusBarFrame.size.height; 我覺得這句話比較重要,它將view的位置往下移動(dòng)了20point(point在非retina屏幕中是20px,在retina屏幕中是40px),這樣就不會(huì)擋住iphone頂部的狀態(tài)欄了。

switchView.frame = switchViewFrame; 將修改過的frame從新賦值給switchView

[self.window addSubview:switchView]; 將switchView設(shè)置成window的subview。怎么理解這句話呢,就是說,一個(gè)app只有一個(gè)窗口(window),這個(gè)window只能同時(shí)顯示一個(gè)view,且這個(gè)view是基于這個(gè)window而存在的,是放在這個(gè)window里面的,因此稱之為window的subview,子視圖。

5)編輯BIDSwitchViewController.h BIDSwitchViewController是root controller,用于控制其他2個(gè)view的切換,因此需要在其頭文件中聲明其他兩個(gè)controller,然后需要定義一個(gè)Action,用來完成對2個(gè)view的切換,將BIDSwitchViewController.h修改成如下

復(fù)制代碼
#import <UIKit/UIKit.h>@class BIDBlueViewController;@class BIDYellowViewController;@interface BIDSwitchViewController : UIViewController@property (strong, nonatomic) BIDBlueViewController *blueViewController;@property (strong, nonatomic) BIDYellowViewController *yellowViewController;- (IBAction)switchViews:(id)sender;@end
復(fù)制代碼

代碼還是很好理解的,和前面在BIDAppDelegate.h中添加BIDSwitchViewController是一樣的。

6)關(guān)聯(lián)BIDSwitchViewController和SwitchView.xib 在Project Navigator中選中SwitchView.xib,在xib的dock中選中File's Owner

然后在Identity inspector中將Class改成BIDSwitchViewController 這樣就將SwitchView.xib關(guān)聯(lián)到了BIDSwitchViewController,如果我們選擇Connections inspector,會(huì)看到我們剛才在BIDSwitchViewController.h中定義的Action:switchViews出現(xiàn)在Received Actions,我們之后就可以將這個(gè)Action關(guān)聯(lián)到SwitchView.xib的控件上。

7)在SwitchView.xib上添加Toolbar 在這個(gè)例子總,我們切換veiw的方式是點(diǎn)擊Toolbar上的一個(gè)button,然后切換2個(gè)view,SwitchView.xib使我們的root view,因此我們需要在SwitchView.xib上添加一個(gè)toolbar,然后點(diǎn)擊toolbar上的按鈕,切換BlueView.xib和YellowView.xib。

選中SwitchView.xib,在object library中找到Toolbar 拖動(dòng)到View上,放在最下方 默認(rèn)的,已經(jīng)有一個(gè)button在Toolbar上了,雙擊button改變文字,將文字改成“Switch Views” 接著就是將“Switch Views”按鈕關(guān)聯(lián)到switchViews,選中“Switch Views”,control-dragged到File's Owner,在彈出的框中選中switchViews 打開Connections inspector,我們可以看到關(guān)聯(lián)后的情況 有一個(gè)不同的地方,Toolbar上的button不像一般的button,會(huì)有很多的方法讓你進(jìn)行關(guān)聯(lián),Toolbar上button的Sent Actions(其他的button叫做Send Events,有很多個(gè)方法)只有一個(gè)方法,而它的作用相當(dāng)于一般button的touch up inside。

8)關(guān)聯(lián)SwitchView.xib和BIDSwitchViewController's view outlet BIDSwitchViewController繼承自UIViewController,在UIViewController中有一個(gè)outlet view,另外當(dāng)我們在做第6)步的時(shí)候,將SwitchView.xib的class改成了BIDSwitchViewController,所以我們要將這個(gè)view關(guān)聯(lián)到SwitchView.xib,關(guān)聯(lián)的方法是選中SwitchView.xib,然后選中File's Owner,control-drag到下面的View 釋放鼠標(biāo)后,在填出的框中選則view,這樣就關(guān)聯(lián)好了。 關(guān)聯(lián)好后,查看Connections inspector,也可以看到關(guān)聯(lián)后的結(jié)果

9)編輯BIDSwitchViewController.m 添加如下代碼

復(fù)制代碼
#import "BIDSwitchViewController.h"#import "BIDYellowViewController.h"#import "BIDBlueViewController.h"@implementation BIDSwitchViewController@synthesize yellowViewController;@synthesize blueViewController;
復(fù)制代碼

2個(gè)#import這個(gè)很好理解,因?yàn)锽IDSwitchViewController是root controller,會(huì)控制另外2個(gè)controller,因此需要把另外2個(gè)controller引入進(jìn)來,這樣才可以對他們進(jìn)行操作。 2個(gè)synthesize對著頭文件中的2個(gè)property。

復(fù)制代碼
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad{    self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];    [self.view insertSubview:self.blueViewController.view atIndex:0];    [super viewDidLoad];}
復(fù)制代碼

首先去掉viewDidLoad的注釋,然后添加以上的代碼。從最后一句可以看出,viewDidLoad方法繼承自UIViewController,在這里對其重載,這個(gè)方法發(fā)生在當(dāng)view已經(jīng)載入完成后,我們所要做的是當(dāng)root view載入完成后,在載入root view的subview,在我們的這個(gè)例子中是BlueView。BlueView的載入方法和前面的一樣,使用initWithNibName,然后作為subView插入到當(dāng)前的view中(當(dāng)前的view就是root view,也就是SwitchView)。

復(fù)制代碼
- (IBAction)switchViews:(id)sender{    if(self.yellowViewController.view.superview == nil) {        if(self.yellowViewController == nil) {            self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];        }        [blueViewController.view removeFromSuperview];        [self.view insertSubview:self.yellowViewController.view atIndex:0];    } else {        if (self.blueViewController ==nil) {            self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];        }        [yellowViewController.view removeFromSuperview];        [self.view insertSubview:self.blueViewController.view atIndex:0];    }}
復(fù)制代碼

實(shí)現(xiàn)switchViews Action,上面的代碼還是很好理解的,首先判斷當(dāng)前哪個(gè)subview是沒有superview的,因?yàn)檫@2個(gè)subview不會(huì)同時(shí)顯示,當(dāng)blueSubview顯示時(shí),YellowSubview就會(huì)從root view中移除,因此不會(huì)具有superview,當(dāng)?shù)弥莻€(gè)subview沒有superview,就說明應(yīng)該顯示這個(gè)subview。知道該顯示哪個(gè)subview后,再判斷這個(gè)subview是否還存在(是否需要重新載入初始化),然后將另一個(gè)subview從superview中移除,再將subview顯示出來。

復(fù)制代碼
- (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.    if (self.blueViewController.view.superview == nil){        self.blueViewController = nil;    } else {        self.yellowViewController = nil;    }}
復(fù)制代碼

當(dāng)ios的內(nèi)存不夠用時(shí),didReceiveMemoryWarning會(huì)被系統(tǒng)自動(dòng)調(diào)用,來釋放可利用的內(nèi)存。在這里如果哪個(gè)subview沒有顯示,就釋放該subview,騰出內(nèi)存。

至此BIDSwitchViewController的所有代碼都寫好了,下面就應(yīng)該處理BIDBlueVeiwController和BIDYellowViewController了。

(友情提示,時(shí)不時(shí)的編譯一下你的project,盡早發(fā)現(xiàn)問題,容易修改,否則到后面自己都不知道錯(cuò)在哪里)

10)編輯BlueView.xib和YellowView.xib 分別在BlueView.xib和YellowView.xib上添加一個(gè)button,并命名為“Press me”。

修改BlueView.xib的class為BIDBlueViewController,修改YellowView.xib的class為BIDYellowController(修改方法:選中xib,點(diǎn)擊File's Owner,在Identity inspector中更改class)   class改變了,就需要重新關(guān)聯(lián)一下File's owner的view,方法和之前的一樣,選中File‘s Owner,control-drag到下面的View上,在彈出的框中選擇view,關(guān)聯(lián)完畢,2個(gè)xib都需要進(jìn)行這個(gè)操作。

在Attributes inspector中,將BlueView.xib的background顏色改成藍(lán)色 同樣的方法將YellowView.xib的background顏色改成黃色

還有一個(gè)地方需要設(shè)置,因?yàn)槲覀兪窃趓oot view中顯示這2個(gè)subview,而root view有一個(gè)toolbar在最下方,因此subview需要把toolbar的位置空出來,再進(jìn)行自己的布局,還是打開Attributes inspector,在Simulated Metrics欄中有很多現(xiàn)象,他們都是用來模擬iphone屏幕上各種占位控件的,我們將Button Bar的選項(xiàng)設(shè)成Toolbar,這樣xib就會(huì)空出Toolbar的位置來進(jìn)行布局計(jì)算了 仔細(xì)看的話,“Press me”按鈕的位置往上移動(dòng)了那么一點(diǎn)點(diǎn)

11)在BIDBlueViewController和BIDYellowViewController中添加代碼 在BIDBlueViewController.h中添加Action,如下

@interface BIDBlueViewController : UIViewController- (IBAction)blueButtonPressed;@end

在BIDBlueViewController.m中實(shí)現(xiàn)blueButtonPressed,如下

復(fù)制代碼
- (IBAction)blueButtonPressed{    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];}
復(fù)制代碼

在BIDYellowViewController.h添加Action,如下

@interface BIDYellowViewController : UIViewController- (IBAction)yellowButtonPressed;@end

在BIDYellowViewController.m中實(shí)現(xiàn)yellowButtonPressed,如下

復(fù)制代碼
- (IBAction)yellowButtonPressed{    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];}
復(fù)制代碼

代碼都很簡單,就不多做解釋了。

12)關(guān)聯(lián)button和Action 打開BlueView.xib,選中“Press me”按鈕,control-drag到File's owner釋放,在填出的框中選擇blueButtonPressed。 打開YellowView.xib,選中“Press me”按鈕,control-drag到File's owner釋放,在填出的框中選擇yellowButtonPressed。

13)編譯運(yùn)行 至此我們已經(jīng)可以編譯運(yùn)行程序了,編譯成功后,iphone模擬器中顯示的效果(“Press me”按鈕的效果就不演示了)

按下“Switch Views”按鈕,BlueSubview會(huì)切換到Y(jié)ellowSubview

14)更炫的切換view的方法 有沒有發(fā)現(xiàn)上面切換view效果很無聊,ios中有更炫的切換view的方法,使用動(dòng)畫的方式切換view,打開BIDSwitchViewController.m,重新編輯switchViews方法,如下

復(fù)制代碼
- (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) {            self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];        }        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];        [blueViewController.view removeFromSuperview];        [self.view insertSubview:self.yellowViewController.view atIndex:0];    } else {        if (self.blueViewController ==nil) {            self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];        }        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];        [yellowViewController.view removeFromSuperview];        [self.view insertSubview:self.blueViewController.view atIndex:0];    }    [UIView commitAnimations];}
復(fù)制代碼

[UIViewbeginAnimations:@"View Flip" context:nil]; 先可以不用理解這句話的意思,因?yàn)槲乙矝]有理解,反正這行代碼是聲明一個(gè)animation block,前一個(gè)參數(shù)是設(shè)置animation block的title,后一個(gè)參數(shù)是設(shè)置一個(gè)對象,我也搞不清楚是干什么的,大概在以后的學(xué)習(xí)中會(huì)有所了解。

[UIViewsetAnimationDuration:1.25]; 設(shè)定動(dòng)畫時(shí)間,即切換view的時(shí)間

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut]; 設(shè)定動(dòng)畫的運(yùn)動(dòng)方式,開始慢,中間快,最后慢,大家開始看ios自己的說明吧。(An ease-in ease-out curve causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. This is the default curve for most animations.)

[UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 設(shè)定切換的樣式,一共有4個(gè)值可以選: UIViewAnimationTransitionFlipFromRight UIViewAnimationTransitionFlipFromLeft UIViewAnimationTransitionFlipCurUp UIViewAnimationTransitionFlipCurDown 這個(gè)大家自試試就可以知道了

[UIViewcommitAnimations]; 當(dāng)所有動(dòng)畫的值設(shè)置完畢后,提交動(dòng)畫,之后view就會(huì)按照設(shè)定的動(dòng)畫進(jìn)行view的切換了。 (此圖截于書本,因此不同清晰)

15)總結(jié) ok,所有的功能都已經(jīng)完成了,在這個(gè)例子中,我們使用toolbar來完成對不同view的切換,我們需要一個(gè)root view進(jìn)行總的控制,然后多個(gè)subview進(jìn)行切換,最后還使用一個(gè)比較炫的效果進(jìn)行view之間的轉(zhuǎn)換,內(nèi)容很充實(shí)!

View Switcher


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 常宁市| 祁连县| 西盟| 洛扎县| 虎林市| 白朗县| 大厂| 莆田市| 鹿泉市| 旬阳县| 循化| 遵化市| 克什克腾旗| 开江县| 阳原县| 清原| 潮州市| 宁津县| 灵璧县| 卓资县| 尤溪县| 伊吾县| 永安市| 松溪县| 车致| 郴州市| 萨迦县| 镇康县| 英超| 平陆县| 宝山区| 建宁县| 海丰县| 雷州市| 米林县| 湘阴县| 洛扎县| 广河县| 新绛县| 西华县| 岐山县|