繁体   English   中英

以编程方式Swift设置初始视图控制器

[英]Setting Initial View Controller Programmatically Swift

我在堆栈溢出时看到了很长的Objective-C答案,但没有快速的答案。

如何从视图控制器以swift方式以编程方式更改初始视图控制器?

我认为它会是这样的:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
storyboard.setInitialViewController(identifier: ViewController())

但不,这没有任何作用。 第一行很好,但第二行的功能不存在。

要在视图控制器中而不是在应用程序委托中执行:只需在视图控制器中获取对AppDelegate的引用,并使用右视图控制器重置它的窗口对象,因为它是rootviewController。

第1步 :制作一些用户可以调整的NSUserDefaults。 几个按钮,表视图中的一些开关,一些东西。 然后,当用户点击按钮时,我们更改NSUserDefault。

@IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) {
  defaults.setObject("ViewController2", forKey: "LaunchView")
}
@IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) {
  defaults.setObject("ViewController1", forKey: "LaunchView")
}

将设置视图控制器中的几个按钮挂钩到这些功能,我们已经开始了。

第2步 :为您希望能够设置为启动视图的所有故事板设置故事板ID。 因此,对于每个可能是初始视图控制器的View Controller:

- 进入你的故事板。

- 单击视图控制器。

- 在右侧边栏中,单击您可以控制课程的类似报纸的图标。

- 在“身份”部分(第三行)中,选中“使用故事板ID”(确保它已打开),然后在“故事板ID”文本字段中键入类似“VC1”的内容。 确保为每个视图控制器选择不同的Storyboard ID。

- 为每个视图控制器重复。

第3步 :在AppDelegate.swift文件中设置初始视图控制器。 进入func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool你的app委托的func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool部分。

添加此内容以从先前创建的NSUserDefault中读取:

let defaults = NSUserDefaults.standardUserDefaults()
    if let launchview = defaults.stringForKey("LaunchView")
    {

}

这将查找名为“LaunchView”的NSUserDefault字符串(您在步骤1中创建),并在找到匹配的NSUserDefault时将其设置为新变量launchview。

然后,在if let launchview...括号内,我们想要检查你将LaunchView设置LaunchView 对于您在步骤1中设置为LaunchView的每个对象(在示例中,我执行了"ViewController2""ViewController1" ),您必须在此处检查它。 所以,在这些括号内,我们添加:

if launchview == "ViewController2" {

} else if launchview == "ViewController1" {

}

然后,在每个if语句中,我们添加以下代码:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) // this assumes your storyboard is titled "Main.storyboard"
let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YOUR_VC_IDENTIFIER") as! YourViewController // inside "YOUR_VC_IDENTIFIER" substitute the Storyboard ID you created in step 2 for the view controller you want to open here. And substitute YourViewController with the name of your view controller, like, for example, ViewController2.
appDelegate.window?.rootViewController = yourVC
appDelegate.window?.makeKeyAndVisible()

当应用程序在后台运行一段时间后完成加载时,这将打开所选窗口。

didFinishLoadingWithOptions完成的didFinishLoadingWithOptions部分可能如下所示:(不要只是复制和粘贴,请阅读上面的说明)

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

let defaults = NSUserDefaults.standardUserDefaults()
        if let launchview = defaults.stringForKey("LaunchView")
        {

            if launchview == "ViewController1" {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1
        appDelegate.window?.rootViewController = yourVC
        appDelegate.window?.makeKeyAndVisible()

            } else if launchview == "ViewController2" {
                let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds)
                let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1
                appDelegate.window?.rootViewController = yourVC
                appDelegate.window?.makeKeyAndVisible()
            }

        }

        return true
   }

我希望这对你有所帮助,非常感谢Ankit Goel,他帮我解决了这个问题。 阅读下面的评论了解更多信息。

最后一点说明 :如果您在设置视图中使用开关,请确保在您从NSUserDefault LaunchView读取的设置视图控制器的viewDidLoad中,用户最后选择了哪一个。

针对Swift 3进行了更新

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    window?.rootViewController = UINavigationController(rootViewController: ViewController())

    return true
}

首先,可以在应用程序中以编程方式设置应用程序的初始视图控制器application:didFinishLaunchingWithOptions:使用问题中公开的代码:

您可以根据条件管理所需的所有条件,以显示一个或另一个UIViewController

例如,假设您希望在您的应用程序中仅在第一次安装应用程序时显示演练,之后再浏览另一个登录屏幕,然后再显示另一个,但您只显示第一次演练和登录以防不在记录之前和另一个。

当然,这可以用几种方式处理,我只是试着向你解释一下制作方法。

为此,您可以设置一个名为SplashViewController的控制器,它是您的初始UIViewController ,在其中您可以显示应用程序的图像(大图像徽标,启动屏幕)以及当您前往某个地方时的过程。 通过这种方式,您可以清理AppDelegate的大量代码,并且可以更轻松地对其进行单元测试。

现在,如果你想要去到另一个UIViewController从另一个里面UIViewController ,你可以使用下面的代码做到这一点:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("ControllerName") as! ControllerName
self.presentViewController(viewController, animated: true, completion: nil)

我希望这对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM