您的位置:首页 > 移动开发 > IOS开发

ios开发-控制器的创建方式

2015-04-12 22:44 295 查看

1.文件结构



Two.storyboard



MJThree5345.xib



2.MJAppDelegate.m

//  MJAppDelegate.m

#import "MJAppDelegate.h"
#import "MJTwoViewController.h"
#import "MJOneViewController.h"
#import "MJThreeViewController.h"

// 蓝色\灰色

@implementation MJAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //window的大小占据整个窗口

self.window.backgroundColor = [UIColor whiteColor];

//    MJThreeViewController *three = [[MJThreeViewController alloc] initWithNibName:@"MJThree5345" bundle:nil];
//    self.window.rootViewController = three;
//    [self test1];
[self test2];
[self.window makeKeyAndVisible];

return YES;

}

- (void)test1
{

// 加载storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Two" bundle:nil]; //nil默认就是mbundle

// 创建storyboard里面灰色的控制器
//    UIViewController *vc = [storyboard instantiateInitialViewController];
//就是箭头所指的那个控制器    件storyboard的class属性便知道返回的是个 UIViewController类型的对象
// MJTwoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"pink"];
MJTwoViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"pink"];

self.window.rootViewController = vc;

NSLog(@"%@", vc);

}
-(void)test2
{
MJOneViewController *one = [[MJOneViewController alloc] init];
one.view.backgroundColor = [UIColor blueColor];
self.window.rootViewController = one;

MJTwoViewController *two = [[MJTwoViewController alloc] init];
two.view.backgroundColor = [UIColor blueColor];
self.window.rootViewController = two;
}
- (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, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the 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. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


3.运行结果

3.1调用test2方法运行结果

[selftest2];



3.2调用test1方法运行结果

[selftest1];



3.3代码创建

MJThreeViewController *three = [[MJThreeViewControlleralloc]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: