以下是在iOS中最簡單的界面切換示例。使用了多個Controller,并演示Controller之間在切換界面時的代碼處理。
實現(xiàn)的應(yīng)用界面:
?
首先,創(chuàng)建一個window-based application,即:
?
?
使用window-base application的目的是,盡量從最基本的情況下說明程序的編寫過程。項目創(chuàng)建好后,即可以編譯運行,執(zhí)行結(jié)果是白屏頁面:
?
?
編寫第一個視圖和控制器,我管它叫Topic,即主題,因此控制器命名為:TopicController,視圖TopicView。
創(chuàng)建TopicController:
?
?
這樣將創(chuàng)建3個文件:
?
?
視圖xib文件也一同創(chuàng)建了。而且:
?
?
會自動生成File’s Owner的Class。
在MainWindow.xib中,將剛剛創(chuàng)建的控制器(TopicController)加進來。
先要拖拽一個View Controller進來:
?
?
然后,給View Controller改名:
?
?
下面,要把這個Controller設(shè)置給WindowDemoAppDelegate。在它的頭文件中:
#import #import "TopicController.h" @interface WindowDemoAppDelegate : NSObject { UIWindow *window; IBOutlet TopicController *topicController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
?
在實現(xiàn)文件中:
#import "WindowDemoAppDelegate.h" @implementation WindowDemoAppDelegate @synthesize window; #pragma mark – #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self.window addSubview:topicController.view]; [self.window makeKeyAndVisible]; return YES; }
?
然后,為了看的清楚,把TopicController.xib文件中的視圖顏色改為綠色:
?
?
運行應(yīng)用,會看到如下效果:
?
?
為該界面添加一個按鈕:
?
?
為該按鈕創(chuàng)建處理方法。在TopicController的頭文件中:
#import <UIKit/UIKit.h> @interface TopicController : UIViewController { } -(IBAction) getDetail:(id)sender; @end
?
在實現(xiàn)文件中:
#import "TopicController.h" @implementation TopicController -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …"); }
?
在IB中,將按鈕和控制器的Action連接:
?
?
再次運行應(yīng)用,可看到日志中的打印內(nèi)容:
?
?
按照上面創(chuàng)建Controller的方法,再創(chuàng)建一個DetailController。先把DetailController.xib的視圖設(shè)置顏色,為了以后調(diào)試觀察識別。
?
?
#import "TopicController.h" @implementation TopicController -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …"); }
?
基本思路是找到window實例,可通過window的rootViewController屬性設(shè)置新的控制器實例(比如DetailController),取代TopicController。代碼可這樣寫:
#import "TopicController.h" #import "DetailController.h" @implementation TopicController -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …, window.views: %@",self.view.window.subviews); DetailController *detailController=[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil]; self.view.window.rootViewController=detailController; NSLog(@"window.views: %@",detailController.view.window.subviews); }
?
加上這部分代碼后,點擊按鈕就可生效,產(chǎn)生這樣的效果:
?
?
上面的代碼做一下解釋:
- 首先創(chuàng)建了一個新的DetailController實例
- 然后,當(dāng)前的controller的view屬性,可以獲得window實例,通過window實例的rootViewController屬性的設(shè)置,將當(dāng)前的控制器替換為新的控制器
- window對象是一個非常重要的類,你可以把它看作ios開發(fā)的畫框,視圖是放在畫框里的,window有個subvews列表,里面可以存放多個View
- 當(dāng)設(shè)置window.rootViewController屬性的時候,window會自動將該屬性的UIViewController的view添加到window的subview中,這也是為什么日志中打印的window.subviews列表中有兩個實例的原因
這個代碼很不完備,比如存在內(nèi)存泄漏,需要這樣:
DetailController *detailController=[[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil] autorelease];
?
因為,這個detailController這句話后,計數(shù)器為1了,再賦值給window.rootViewController屬性,就是2了。因此這里要做自動釋放。
?
這個代碼還有個問題,就是看上去很別扭,在一個控制器代碼里去創(chuàng)建另一個控制器。這一方面很容易出問題,另一方面,代碼的結(jié)構(gòu)不清晰。下面用委托模式給代碼解耦,也為下一步做返回按鈕做準(zhǔn)備。
委托模式,一般用protocol來實現(xiàn)。先寫個protocol:
#import <UIKit/UIKit.h> @protocol SwitchViewDelegate -(void)getDetail; @end
?
然后,需要讓UIApplicationDelegate實現(xiàn)類實現(xiàn)該protocol:
#import <UIKit/UIKit.h> #import "TopicController.h" #import "SwitchViewDelegate.h" @interface WindowDemoAppDelegate : NSObject <UIApplicationDelegate,SwitchViewDelegate> { UIWindow *window; IBOutlet TopicController *topicController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
?
在實現(xiàn)類中:
#import "WindowDemoAppDelegate.h" #import "DetailController.h" @implementation WindowDemoAppDelegate @synthesize window; #pragma mark – #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self.window addSubview:topicController.view]; topicController.delegate=self; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. */ } - (void)applicationDidEnterBackground:(UIApplication *)application { /* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */ } - (void)applicationWillEnterForeground:(UIApplication *)application { /* Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background. */ } - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. See also applicationDidEnterBackground:. */ } #pragma mark – #pragma mark Memory management - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { /* Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. */ } - (void)dealloc { [window release]; [super dealloc]; } -(void)getDetail{ DetailController *detailController=[[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil] autorelease]; self.window.rootViewController=detailController; } @end
?
另外,就是要為控制器里增加delegate屬性,頭文件:
#import <UIKit/UIKit.h> #import "SwitchViewDelegate.h" @interface TopicController : UIViewController { id<SwitchViewDelegate> delegate; } @property(nonatomic,retain) id<SwitchViewDelegate> delegate; -(IBAction) getDetail:(id)sender; @end
?
實現(xiàn)文件:
#import "TopicController.h" #import "DetailController.h" @implementation TopicController @synthesize delegate; -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …, window.views: %@",self.view.window.subviews); [delegate getDetail]; }
?
實現(xiàn)的效果和上面的是類似的,但是引入委托模式后,代碼的架構(gòu)就比較清楚了,利于以后的維護。
原文鏈接:
http://marshal.easymorse.com/archives/4415
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
