ios遵守mvc设计模式,下面介绍创建控制器viewcontroller的几种方法,来实现页面的跳转
1.纯代码创建
//
// AppDelegate.m
// study2024
//
// Created by zhifei zhu on 2024/8/7.
//
#import "AppDelegate.h"
#import "MyViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//使用自定义viewcontroller,先删除main.storyboard和viewcontroller
//1.创建窗口并指定大小
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//2.创建控制器
MyViewController *myViewController=[[MyViewController alloc]init];
myViewController.view.backgroundColor=[UIColor greenColor];
//3.设置为窗口的根控制器
self.window.rootViewController=myViewController;
//4.将窗口作为应用程序的主窗口
[self.window makeKeyWindow];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
2. 通过storyboard创建控制器
1)、Is initial view controller打上勾
//通过storyboard创建控制器
UIStoryboard *uiStoryboard=[UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
//storyboard只有一个viewcontroller的情况
// MyViewController *myVc=[uiStoryboard instantiateInitialViewController];
//storyboard有多个viewcontroller的情况,需要使用storyboard id属性
MyCatViewController *myVc=[uiStoryboard instantiateViewControllerWithIdentifier:@"MyStoryboard1"];
[self.view.window setRootViewController:myVc];
2.3、通过xib创建控制器
//创建控制器的时候同时创建xib页面
PhotoViewController * myVc = [[PhotoViewController alloc]init];
[self.view.window setRootViewController:myVc];