以下是在iOS中最簡(jiǎn)單的界面切換示例。使用了多個(gè)Controller,并演示Controller之間在切換界面時(shí)的代碼處理。
實(shí)現(xiàn)的應(yīng)用界面:
?
首先,創(chuàng)建一個(gè)window-based application,即:
?
?
使用window-base application的目的是,盡量從最基本的情況下說明程序的編寫過程。項(xiàng)目創(chuàng)建好后,即可以編譯運(yùn)行,執(zhí)行結(jié)果是白屏頁(yè)面:
?
?
編寫第一個(gè)視圖和控制器,我管它叫Topic,即主題,因此控制器命名為:TopicController,視圖TopicView。
創(chuàng)建TopicController:
?
?
這樣將創(chuàng)建3個(gè)文件:
?
?
視圖xib文件也一同創(chuàng)建了。而且:
?
?
會(huì)自動(dòng)生成File’s Owner的Class。
在MainWindow.xib中,將剛剛創(chuàng)建的控制器(TopicController)加進(jìn)來。
先要拖拽一個(gè)View Controller進(jìn)來:
?
?
然后,給View Controller改名:
?
?
下面,要把這個(gè)Controller設(shè)置給WindowDemoAppDelegate。在它的頭文件中:
#import #import "TopicController.h" @interface WindowDemoAppDelegate : NSObject { UIWindow *window; IBOutlet TopicController *topicController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
?
在實(shí)現(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ùn)行應(yīng)用,會(huì)看到如下效果:
?
?
為該界面添加一個(gè)按鈕:
?
?
為該按鈕創(chuàng)建處理方法。在TopicController的頭文件中:
#import <UIKit/UIKit.h> @interface TopicController : UIViewController { } -(IBAction) getDetail:(id)sender; @end
?
在實(shí)現(xiàn)文件中:
#import "TopicController.h" @implementation TopicController -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …"); }
?
在IB中,將按鈕和控制器的Action連接:
?
?
再次運(yùn)行應(yīng)用,可看到日志中的打印內(nèi)容:
?
?
按照上面創(chuàng)建Controller的方法,再創(chuàng)建一個(gè)DetailController。先把DetailController.xib的視圖設(shè)置顏色,為了以后調(diào)試觀察識(shí)別。
?
?
#import "TopicController.h" @implementation TopicController -(IBAction) getDetail:(id)sender{ NSLog(@"get detail …"); }
?
基本思路是找到window實(shí)例,可通過window的rootViewController屬性設(shè)置新的控制器實(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); }
?
加上這部分代碼后,點(diǎn)擊按鈕就可生效,產(chǎn)生這樣的效果:
?
?
上面的代碼做一下解釋:
- 首先創(chuàng)建了一個(gè)新的DetailController實(shí)例
- 然后,當(dāng)前的controller的view屬性,可以獲得window實(shí)例,通過window實(shí)例的rootViewController屬性的設(shè)置,將當(dāng)前的控制器替換為新的控制器
- window對(duì)象是一個(gè)非常重要的類,你可以把它看作ios開發(fā)的畫框,視圖是放在畫框里的,window有個(gè)subvews列表,里面可以存放多個(gè)View
- 當(dāng)設(shè)置window.rootViewController屬性的時(shí)候,window會(huì)自動(dòng)將該屬性的UIViewController的view添加到window的subview中,這也是為什么日志中打印的window.subviews列表中有兩個(gè)實(shí)例的原因
這個(gè)代碼很不完備,比如存在內(nèi)存泄漏,需要這樣:
DetailController *detailController=[[[DetailController alloc] initWithNibName:@"DetailController" bundle:nil] autorelease];
?
因?yàn)椋@個(gè)detailController這句話后,計(jì)數(shù)器為1了,再賦值給window.rootViewController屬性,就是2了。因此這里要做自動(dòng)釋放。
?
這個(gè)代碼還有個(gè)問題,就是看上去很別扭,在一個(gè)控制器代碼里去創(chuàng)建另一個(gè)控制器。這一方面很容易出問題,另一方面,代碼的結(jié)構(gòu)不清晰。下面用委托模式給代碼解耦,也為下一步做返回按鈕做準(zhǔn)備。
委托模式,一般用protocol來實(shí)現(xiàn)。先寫個(gè)protocol:
#import <UIKit/UIKit.h> @protocol SwitchViewDelegate -(void)getDetail; @end
?
然后,需要讓UIApplicationDelegate實(shí)現(xiàn)類實(shí)現(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
?
在實(shí)現(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
?
實(shí)現(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]; }
?
實(shí)現(xiàn)的效果和上面的是類似的,但是引入委托模式后,代碼的架構(gòu)就比較清楚了,利于以后的維護(hù)。
原文鏈接:
http://marshal.easymorse.com/archives/4415
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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