在 appdelegate 中设置初始视图控制器 - swift [英] set initial viewcontroller in appdelegate - swift

查看:22
本文介绍了在 appdelegate 中设置初始视图控制器 - swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 appdelegate 设置初始视图控制器.我找到了一个非常好的答案,但它是在 Objective C 中,我在 swift 中无法实现相同的目标.

I would like to set the initial viewcontroller from the appdelegate. I found a really good answer, however it's in Objective C and im having trouble achieving the same thing in swift.

使用故事板以编程方式设置初始视图控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = // determine the initial view controller here and instantiate   it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

有人可以帮忙吗?

我希望初始 Viewcontroller 依赖于使用条件语句满足的某些条件.

I want the initial Viewcontroller to be dependent on certain conditions being met using a conditional statement.

推荐答案

Xcode11 和 SceneDelegate 注:

从Xcode11开始,因为SceneDelegate,很可能你不应该在AppDelegate里面做.而是从 SceneDelegate 开始.有关更多信息,请参阅其他答案

Xcode11 and SceneDelegate note:

Starting from Xcode11, because of SceneDelegates, it's likely that you shouldn't do it inside AppDelegate. Instead do it from SceneDelegate. For more on that see this other answer

我使用这个线程来帮助我将目标 C 转换为 swift,并且它工作得很好.

I used this thread to help me convert the objective C to swift, and its working perfectly.

在 Swift 中实例化并呈现一个 viewController

Swift 2 代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")
    
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    
    return true
}

Swift 3 代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    
    let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}

这篇关于在 appdelegate 中设置初始视图控制器 - swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆