微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据条件打开第一个视图

如何解决根据条件打开第一个视图

我希望我的应用在启动时有条件地检查变量是否正确。基于此,它应该转到简介屏幕(在我的情况下,他可以在其中选择一个变量,然后选择一个团队),或者应该启动主视图。搜索后,我找到了此代码并进行了编辑。

但是似乎仍然存在问题。首先,我没有两个标识符。简介有一个主视图,但没有主视图。我的主视图称为WeatherViewController,“简介”屏幕称为FirstScreenViewController。

我还添加了Main.storyboard的图片。 我也用Google搜索了很多关于条件UINavigationController的信息,但是我只能通过视频来理解,但是没有找到关于它的视频。

我尝试使用here中的代码

main.Storyboard

 var id = hello ? "goToIntro" : "???"
      self.window = UIWindow(frame: UIScreen.main.bounds)
      let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
      let WeatherViewController: UIViewController = mainStoryboard.FirstScreenViewController(withIdentifier: WVC has no identifier??) as UIViewController
      self.window?.rootViewController = WeatherViewController
      self.window?.makeKeyAndVisible()
       if hello {
          self.performSegue(withIdentifier: "goToIntro",sender: self)
        } else {
    /here nothing should happen. It should open the Main View
          self.performSegue(withIdentifier: "???",sender: self)
        }

解决方法

只需重复您创建segue goToIntro的步骤即可。

  1. 在情节提要中添加新的UIViewControllerMainViewController)。
  2. 添加segue并为其指定一个标识符。
  3. 在帖子中显示的代码的else条件中提供标识符值,以对刚创建的UIViewController执行隔离。

最终代码应如下所示:

var id = hello ? "goToIntro" : "goToMain"
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
let mainStoryboard = UIStoryboard(name: "Main",bundle: nil)
let weatherViewController = mainStoryboard.instantiateInitialViewController()
window?.rootViewController = weatherViewController
window?.makeKeyAndVisible()
weatherViewController.performSegue(withIdentifier: id,sender: self)

您不需要if else块,因为已经使用id三元运算符确定了?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。