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;
}
運行結果: