IOS開發(6)之UIViewController

來源:互聯網
上載者:User

1 前言
IOS開發遵循MVC模型,即模型-視圖-控制器。

視圖是展現給使用者的東西;模型是App管理的資料,也是App引擎的另一種叫法;控制器則是串連模型和視圖的橋樑。今天主要介紹一下,在建立跟視圖的ViewController時候,帶nib檔案和不帶nib檔案的區別。

2 UIViewController使用
上delegate的代碼

.h檔案


[plain]
#import <UIKit/UIKit.h> 
#import "RootViewController.h" 
#import "ZYRootViewController.h" 
 
@interface ZYAppDelegate : UIResponder <UIApplicationDelegate> 
 
@property (strong, nonatomic) UIWindow *window; 
//@property (strong, nonatomic) RootViewController *rootViewController; 
@property (strong, nonatomic) ZYRootViewController *rootViewController; 
 
@end 

#import <UIKit/UIKit.h>
#import "RootViewController.h"
#import "ZYRootViewController.h"

@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
//@property (strong, nonatomic) RootViewController *rootViewController;
@property (strong, nonatomic) ZYRootViewController *rootViewController;

@end
.m檔案


[plain]
@synthesize window = _window; 
@synthesize rootViewController; 
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
//    self.window.backgroundColor = [UIColor whiteColor];//去掉背景色方便顯示rootView 
    [self.window makeKeyAndVisible]; 
//    self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:NULL];//無nib檔案方式 
    self.rootViewController = [[ZYRootViewController alloc] initWithNibName:@"ZYRootViewController" bundle:NULL];//有nib檔案方式 
    [self.window addSubview:self.rootViewController.view]; 
    return YES; 

@synthesize window = _window;
@synthesize rootViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
//    self.window.backgroundColor = [UIColor whiteColor];//去掉背景色方便顯示rootView
    [self.window makeKeyAndVisible];
//    self.rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:NULL];//無nib檔案方式
    self.rootViewController = [[ZYRootViewController alloc] initWithNibName:@"ZYRootViewController" bundle:NULL];//有nib檔案方式
    [self.window addSubview:self.rootViewController.view];
    return YES;
}
運行結果:

 
 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.