欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

iOS編寫最簡單的界面切換應用

系統 4188 0

以下是在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

?

iOS編寫最簡單的界面切換應用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩精品视频在线播放 | 国产精品毛片无码 | 国产苐1页影院草草影院 | 欧美一区二区三区视频 | 国产在线观看免费 | 在线色网 | 日本一本免费一二区 | 欧美激情啪啪 | 国产大片线上免费看 | 国产美女自卫慰视频福利 | 67194国产精品免费观看 | 91久久久久久久久久久久久久 | 奇米在线观看视频 | 久久天堂网 | 99精品在线观看 | 久草网在线观看 | 黄色片免费在线播放 | 国产欧美一区二区视频 | 久久国产精品一区二区三区 | cheaperapp.work直接访问 | 男人的午夜影院 | 91官网 | 丝袜诱惑一区二区 | 国产欧美日韩视频在线观看 | 人人舔人人爱 | 国产精品欧美精品 | 久久国产视频网 | 日韩成人在线电影 | 91成人影院未满十八勿入 | 色欧美片视频在线观看 | 免费播放视频 | 99成人精品视频 | 亚洲色图150p | 欧美视频在线一区 | 东方亚洲东方欧美色二区 | 啪一啪| 欧美无乱码久久久免费午夜一区 | 国产91久久精品 | 91蝌蚪九色 | 久久精精 | 国产欧美精品一区二区三区 |