iPhone開(kāi)發(fā)之利用ModalViewController切換View
iPhone開(kāi)發(fā)之利用ModalViewController切換View是本文要介紹的內(nèi)容,當(dāng)程序中含有多個(gè) view,需要在之間切換的時(shí)候,可以使用 UINavigationController,或者是 ModalViewController。UINabigationController 是通過(guò)向?qū)l來(lái)切換多個(gè) view。
而如果 view 的數(shù)量比較少,且顯示領(lǐng)域?yàn)槿恋臅r(shí)候,用 ModalViewController 就比較合適(比如需要用戶輸入信息的view,結(jié)束后自動(dòng)回復(fù)到之前的view)。今天我們就看看 ModalViewController 的創(chuàng)建方法。
ModalViewController 并不像 UINavigationController 是一個(gè)專門的類,使用 UIViewController 的 presentModalViewController 方法指定之后就是 ModalViewController 了。
這里使用上兩回做成的 CustomViewController(由UIViewController繼承)來(lái)實(shí)現(xiàn) ModalViewController 的實(shí)例。
首先,準(zhǔn)備 ModalViewController 退出時(shí)的函數(shù)。調(diào)用 UIViewController 的 dismissModalViewController:Animated: 方法就可以了,如下所示:
- // 這里按鈕按下的時(shí)候退出 ModalViewController
- -(void)dismiss:(id)inSender {
- // 如果是被 presentModalViewController 以外的實(shí)例調(diào)用,parentViewController 將是nil,下面的調(diào)用無(wú)效
- [self.parentViewController dismissModalViewControllerAnimated:YES];
- }
接下來(lái),生成另一個(gè) CustomViewController 的實(shí)例,用來(lái)表示 ModalViewController,并將其對(duì)應(yīng)的 view 設(shè)置成紅色。然后傳遞給 presentModalViewController: Animated: 顯示 ModalViewController 的 view。
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- controller = [[CustomViewController alloc] init];
- [window addSubview:controller.view];
- [window makeKeyAndVisible];
- // 生成 ModalViewController
- CustomViewController* controllerB = [[CustomViewController alloc] init];
- // 設(shè)置 view 的背景為紅色
- controllerB.view.backgroundColor = [UIColor redColor];
- // 顯示 ModalViewController view
- [controller presentModalViewController:controllerB animated:YES];
- // presentModalViewController 已經(jīng)被 controller 管理,這里可以釋放該實(shí)例了
- [controllerB release];
- }
編譯執(zhí)行以后,首先啟動(dòng)的是紅色背景的 ModalViewController view、按下按鈕后恢復(fù)到藍(lán)色背景的通常 view 上。
也可以在顯示 ModalViewController view 之前設(shè)置 UIViewContrller 的 modalTransitionStyle 屬性,使其以動(dòng)畫形式顯示。
- controllerB.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
以上的實(shí)現(xiàn)只是單一地實(shí)現(xiàn)了 ModalViewController view 的功能,除了程序開(kāi)始提醒用戶一些信息外什么也做不了。另外由于是放入了 applicationDidFinishLaunching 中的原因,也不能反復(fù)的顯示。另外,在 ModalViewController view 上設(shè)置的內(nèi)容也不能反映到原來(lái)的 view 上。
接下來(lái)我們將實(shí)現(xiàn)這些功能。
首先,從 ModalViewController view 退出的時(shí)候,需要通知原先的 view。這里使用 iPhone/Cocoa 應(yīng)用程序中經(jīng)常使用的Delegate 設(shè)計(jì)模式(也是推薦使用的)。
實(shí)際上,系統(tǒng)所提供的圖像選擇控制類 UIImagePickerController
或者是參照地址簿時(shí)的ABPeoplePickerNavigationController 類,都用到了 Delegate 模式?;谏弦恢v的中的例子,這里我們追加為3個(gè)按鈕,分別是綠色,灰色和取消。
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor blueColor];
- UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button.frame = CGRectMake(100,100,100,100);
- button.tag = 1;
- [button setTitle:@"綠色" forState:UIControlStateNormal];
- // 按鈕事件對(duì)應(yīng)函數(shù)
- [button addTarget:self action:@selector(dismiss:)
- forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
- button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button.frame = CGRectMake(100,200,100,100);
- button.tag = 2;
- [button setTitle:@"灰色" forState:UIControlStateNormal];
- // 按鈕事件對(duì)應(yīng)函數(shù)
- [button addTarget:self action:@selector(dismiss:)
- forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
- button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button.frame = CGRectMake(100,300,100,100);
- button.tag = 0;
- [button setTitle:@"取消" forState:UIControlStateNormal];
- // 按鈕事件對(duì)應(yīng)函數(shù)
- [button addTarget:self action:@selector(dismiss:)
- forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button];
程序啟動(dòng)的時(shí)候依然是先顯示 ModalViewController view,按下任何一個(gè)按鈕,將關(guān)閉該view。按下“綠色”按鈕,設(shè)置背景為綠色,按下“灰色”按鈕時(shí),設(shè)置背景為灰色。“取消”的時(shí)候什么也不做。
委托處理用下面的函數(shù)實(shí)現(xiàn),當(dāng)參數(shù) inColor 為 nil 的時(shí)候代表取消。
- -(void)selectColor:(UIColor*)inColor;
委托代理的實(shí)例用 id 變量表示。
- @interface CustomViewController : UIViewController {
- id colorSelectDelegate;
- }
設(shè)置該變量的函數(shù)如下。
- -(void)setColorSelectDelegate:(id)inDelegate {
- colorSelectDelegate = inDelegate;
- }
另外如上面 viewDidLoad 所示,按鈕的 tag 分別為0、1、2。按鈕按下時(shí)調(diào)用的函數(shù)中由不同的 tag 來(lái)發(fā)送不同的 UIColor實(shí)例到 colorSelectDelegate 上。
- -(void)dismiss:(id)inSender {
- UIView* view = (UIView*)inSender;
- UIColor* requestColor = nil;
- if (view.tag == 1)
- requestColor = [UIColor greenColor];
- if (view.tag == 2)
- requestColor = [UIColor grayColor];
- [colorSelectDelegate selectColor:requestColor];
- }
這是不使用 UIButton* 而是用 UIView* ,是因?yàn)?tag 屬性被定義在 UIView 類中,不需要必須轉(zhuǎn)換為 UIButton 類。
另外這樣一來(lái),該函數(shù)在 UIButton 以外的情況下也能被使用。
如果想檢查 id 是什么類性的可以使用 isKindOfClass: 方法。接收到具體的參數(shù) inColor 更換背景色,并關(guān)閉 ModalViewController view。
- -(void)selectColor:(UIColor*)inColor {
- if (inColor != nil)
- self.view.backgroundColor = inColor;
- [self dismissModalViewControllerAnimated:YES];
- }
另外,在調(diào)用 presentModalViewController 之前(顯示 ModalViewController view 之前),需要設(shè)定委托的實(shí)例。
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- controller = [[CustomViewController alloc] init];
- [window addSubview:controller.view];
- [window makeKeyAndVisible];
- // 創(chuàng)建 ModalViewController view 的 Controller
- CustomViewController* controllerB = [[CustomViewController alloc] init];
- // 設(shè)置背景色為紅色
- controllerB.view.backgroundColor = [UIColor redColor];
- // 設(shè)置委托實(shí)例
- [controllerB setColorSelectDelegate:controller];
- // 顯示 ModalViewController view
- [controller presentModalViewController:controllerB animated:YES];
- [controllerB release];
編譯一下,程序啟動(dòng)后顯示紅色背景的 ModalViewController view,點(diǎn)擊綠色按鈕后,原先的view的背景變?yōu)榫G色,點(diǎn)擊灰色,顯示灰色的背景,而點(diǎn)擊取消,那么將顯示原先藍(lán)色的背景。
這樣的形式,就是將按鈕的動(dòng)作委托給原先view的 Controller 來(lái)處理了。根據(jù)送來(lái)的 UIColor 來(lái)設(shè)置不同的背景色。
小結(jié):iPhone開(kāi)發(fā)之利用ModalViewController切換View的內(nèi)容介紹完了,希望本文對(duì)你有所幫助!