我正在尝试通过按“感觉幸运”按钮切换到第二个 View 。当我试用我的模拟器并按下它时,它只是进入黑屏。我该如何解决这个问题?
@IBAction func FeelingLuckyPress(sender: UIButton) {
let secondViewController:SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated: true, completion: nil)
}
最佳答案
您可以通过控制从第一个场景顶部的 View Controller 图标拖动到第二个场景,在 Storyboard的两个场景之间添加转场:
然后您可以选择那个 segue 并给它一个唯一的 Storyboard标识符,然后您可以使用该标识符执行 segue:
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 中为该目标场景指定的 Storyboard标识符。
顺便说一句,如果您以编程方式或使用 NIB 创建目标场景,还有其他选择,但 Storyboard更为常见,因此我在上面的示例中重点介绍了它。
关于xcode - 在 Swift 中以编程方式切换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27650140/