使用if else条件语句在Xcode/swift中显示特定的ViewController [英] Display a particular viewcontroller in Xcode/swift using if else conditional statements

查看:70
本文介绍了使用if else条件语句在Xcode/swift中显示特定的ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望a具有条件if语句,以便在应用打开时立即运行.基本上是

I want a to have a conditional if statement to be run immediately when the app is open. Basically, it will be

if x = true {
  //segue to viewcontroller1
} else {
  //stay on this page
}

这将以Xcode进行,并进行快速编码(显然语法是错误的)...如果条件为true并保持在通常情况下的条件,那么将语法写入特定视图控制器的适当方法是什么?可以在打开应用程序时打开?另外,我该放在哪里?我考虑过通常首先显示的viewcontroller中的viewdidload方法,但是需要在加载视图之前检查该变量,以便在条件为true的情况下将视图更改为另一个视图,并且那个条件为而是先打开?

This will be in Xcode and coded in swift (obviously the syntax is wrong)... what is the appropriate way to write the syntax to segue to a particular view controller if the condition is true and stay on the one that normally is opened up to upon opening the app? Also, where do I put this? I considered the viewdidload method in the, normally, first displayed viewcontroller, but the variable needs to be checked before the view loads, such that the view changes to a different one if the condition is true and that one opens first instead?

我试图按如下所示在 AppDelegate.swift 中设置代码:

I tried to set the code in the AppDelegate.swift as follows:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if x{
            let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
            let ViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController = ViewController
        } else{
          //same code as above but to different VC
        }
        return true
    }

但是当我运行它时,我在appDelegate中收到一条错误消息,提示"libc ++ abi.dylib:以NSException类型的未捕获异常终止".修改此代码的正确方法是什么?

But when I run this I get an error in the appDelegate saying that "libc++abi.dylib: terminating with uncaught exception of type NSException". What is the right way to modify this code?

推荐答案

  1. 如果要在演示前选择场景.您可以在 appDelegate.swift 中的 application(应用程序:UIApplication,didFinishLaunchingWithOptions)中添加以下内容:

var id = x ? "id1" : "id2"

self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let exampleVC: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: id) as UIViewController
self.window?.rootViewController = exampleVC
self.window?.makeKeyAndVisible()

确保您在情节提要中命名场景

如果您同意显示第一个场景,然后选择进行顺序拍摄:

If you OK with the first scene showing and then choosing to segue:

if x {
  self.performSegue(withIdentifier: "id1", sender: self)       
} else {
  self.performSegue(withIdentifier: "id2", sender: self)
}

再次,请确保在情节提要中命名这些情节

另一个选项,使用 UINavigationController .将导航控制器设置为您的根视图控制器

Another option, use UINavigationController. Set a Navigation Controller as your root view controller

let id = x ? "id1" : "id2"

let mainStoryboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let exampleVC = mainStoryboard.instantiateViewController(withIdentifier: id) as UIViewController
let navigationController = UINavigationController(rootViewController: exampleVC)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()

这篇关于使用if else条件语句在Xcode/swift中显示特定的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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