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

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