xcode 如何设置初始故事板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45404845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:09:52  来源:igfitidea点击:

How to set initial storyboard

iosobjective-cxcodexcode-storyboard

提问by casillas

I have two storyboards one of them is default called Mainand the other one I have just added is called Admin.

我有两个故事板,其中一个默认称为Main,我刚刚添加的另一个故事板称为Admin

Mainis used for customer, Adminis used for owner of the app.

Main用于客户,Admin用于应用程序的所有者。

I wonder how do you set initial/main interface storyboard programmatically.

我想知道您如何以编程方式设置初始/主界面故事板。

enter image description here

在此处输入图片说明

P.S. I know how to change it via xcode but do not know programatically.

PS 我知道如何通过 xcode 更改它,但不知道如何编程。

回答by matt

You do not set an initial storyboard programmatically.

您不会以编程方式设置初始故事板。

Here's how it works. Either you have a main storyboard listed in Xcode under Main Interface or you don't:

这是它的工作原理。要么您在 Xcode 的 Main Interface 下列出了一个主故事板,要么您没有:

  • If you do, that is the initial storyboard, period. That storyboard is loaded automatically at launch time, and its initial view controller becomes the window's root view controller (and the interface is then displayed automatically).

  • If you don't (that is, if the Main Interface field is empty), then nothinghappens automatically. It is up to your code to obtain a view controller, somehow (possibly from a storyboard), and make its the window's root view controller (and to show the interface).

  • 如果你这样做了,那就是最初的故事板,时期。该情节提要在启动时自动加载,其初始视图控制器成为窗口的根视图控制器(然后自动显示界面)。

  • 如果您不这样做(即,如果 Main Interface 字段为空),则不会自动发生任何事情。以某种方式(可能从情节提要)获取视图控制器并使其成为窗口的根视图控制器(并显示界面)取决于您的代码。

So, to sum up, either everything happens automatically or nothinghappens automatically. There is no intermediate state, as you seem to imagine, in which you can programmatically change things so that a different storyboard is loaded automatically.

所以,总而言之,要么一切自动发生,要么什么都不自动发生。正如您想象的那样,没有中间状态,您可以在其中以编程方式更改内容,以便自动加载不同的故事板。

There is, however, a kind of intermediate state where you permit the Main Interface storyboard to load but then you ignore it. In your implementation of application:didFinishLoading..., you would then sometimesdo the thing I said in the second bullet point, i.e. load a differentview controller and make it the root view controller. This works because the automatic stuff I mentioned in the first bullet point has already happenedby the time application:didFinishLoading...is called. So, in effect, your code sometimes permits that automatic stuff and sometimes overrides it.

但是,有一种中间状态,您允许加载主界面情节提要但随后忽略它。在您的 实现中application:didFinishLoading...,您有时会做我在第二个要点中所说的事情,即加载不同的视图控制器并使其成为根视图控制器。这是有效的,因为我在第一个要点中提到的自动内容在被调用时已经发生application:didFinishLoading...。因此,实际上,您的代码有时会允许这些自动内容,有时会覆盖它。

In this example from my own code, we either use the Main Interface storyboard to load the initial view controller or we load a different view controller from the storyboard ourselves, depending on a value in User Defaults:

在我自己的代码示例中,我们要么使用主界面故事板加载初始视图控制器,要么我们自己从故事板加载不同的视图控制器,具体取决于用户默认值中的值:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if let rvc = self.window?.rootViewController {
        if NSUserDefaults.standardUserDefaults().objectForKey("username") as? String != nil {
            self.window!.rootViewController = rvc.storyboard!.instantiateViewControllerWithIdentifier("root")
        }
    }
    return true
}

回答by samwize

You can set your storyboard programmatically like this in the app delegate:

您可以像这样在应用程序委托中以编程方式设置故事板:

window = UIWindow(frame: UIScreen.main.bounds)
// Or "Admin"
window!.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()! 
window!.makeKeyAndVisible()

You could actually set any view controller. That's how no-storyboard approachis wired up.

您实际上可以设置任何视图控制器。这就是无故事板方法的连接方式。

Oh, another advantage of doing it in code is it delays the storyboard initialization. If you use the "Main Interface" settings in Xcode, the storyboard is actually initialized BEFORE the application:didFinishLaunchingWithOptionsmethod.

哦,在代码中这样做的另一个好处是它延迟了故事板的初始化。如果您在 Xcode 中使用“主界面”设置,则故事板实际上是在application:didFinishLaunchingWithOptions方法之前初始化的。