【问题标题】:Switching Views Programmatically in Swift在 Swift 中以编程方式切换视图
【发布时间】:2014-12-25 19:32:41
【问题描述】:

我正在尝试通过按下“感觉幸运”按钮切换到第二个视图。当我试用我的模拟器并按下它时,它只是进入黑屏。我该如何解决这个问题?

@IBAction func FeelingLuckyPress(sender: UIButton) {

    let secondViewController:SecondViewController = SecondViewController()

    self.presentViewController(secondViewController, animated: true, completion: nil)
}

【问题讨论】:

  • SecondViewController 应该从哪里获得其view?换句话说,你做了什么让这个视图是黑屏?解释。描述。
  • 以下是视图控制器获取其视图的方式。如果不使用其中之一,则视图控制器的视图为空且黑:apeth.com/iOSBook/ch19.html#_view_controller_and_view_creation

标签: xcode swift viewcontroller


【解决方案1】:

您可以通过控制从第一个场景顶部的视图控制器图标拖动到第二个场景来在情节提要中的两个场景之间添加转场:

然后您可以选择该转场并为其指定唯一的故事板标识符,然后您可以使用该标识符执行转场:

performSegue(withIdentifier: "SegueToSecond", sender: self)

或者,您可以执行以下操作:

guard let controller = storyboard?.instantiateViewController(withIdentifier: "Second") as? SecondViewController else {
    return
}
present(controller, animated: true) // or `show(controller, sender: self)`

其中“Second”是我在 Interface Builder 中为该目标场景指定的故事板标识符。


顺便说一句,如果您以编程方式或使用 NIB 创建目标场景,还有其他选择,但故事板更为常见,因此我在上面的示例中重点介绍了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    相关资源
    最近更新 更多