在 App Delegate 中管理两个故事板 [英] Managing two storyboards in App Delegate

查看:22
本文介绍了在 App Delegate 中管理两个故事板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定避免使用自动布局,因此我目前正在尝试实现代码,让我的应用根据屏幕尺寸管理两个不同的故事板.

I have decided to avoid using auto-layout so I am instead currently trying to implement the code to make my app manage two different storyboards based on screen size.

我一直在关注本教程:http://pinkstone.co.uk/how-to-load-a-different-storyboard-depending-on-screen-size-in-ios/

我在尝试将 Objective C 代码翻译成 Swift 时遇到问题.

I am having issues trying to translate the Objective C code into Swift.

这是我目前在 AppDelegate 中的代码:

Here is the code that I currently have in my AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func grabStoryboard() -> UIStoryboard {
    var storyboard = UIStoryboard()
    var height = UIScreen.mainScreen().bounds.size.height

    if height == 480 {
        storyboard = UIStoryboard(name: "Main3.5", bundle: nil)
    } else {
        storyboard = UIStoryboard(name: "Main", bundle: nil)
    }
    return storyboard
}

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    // Override point for customization after application launch.   

    var storyboard: UIStoryboard = self.grabStoryboard()

    self.window?.rootViewController.storyboard.instantiateInitialViewController()
    self.window?.makeKeyAndVisible()

    return true
}

应用程序运行,我没有错误,但是无论我是在 3.5 英寸设备还是 4 英寸设备上运行应用程序,我都只得到 4 英寸的故事板.

The app runs, and I have no errors however no matter whether I run the app on a 3.5 inch device or a 4 inch device, I just get the 4 inch storyboard.

我哪里出错了?

推荐答案

问题在线:

self.window?.rootViewController.storyboard.instantiateInitialViewController()

你应该改用这个:

self.window?.rootViewController = storyboard.instantiateInitialViewController()

我已删除 as UIViewController 因为不再需要它.

I have removed as UIViewController because it is no longer needed.

这篇关于在 App Delegate 中管理两个故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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