xcode - 在 Swift 中以编程方式切换 View

标签 xcode swift viewcontroller

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

@IBAction func FeelingLuckyPress(sender: UIButton) {

    let secondViewController:SecondViewController = SecondViewController()

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

最佳答案

您可以通过控制从第一个场景顶部的 View Controller 图标拖动到第二个场景,在 Storyboard的两个场景之间添加转场:

create segue

然后您可以选择那个 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/

相关文章:

Swift:可选数组的快捷展开

ios - UIStackView 隐藏动画无法正常工作

ios - 推送 View Controller 和添加 Controller View 有什么区别?

ios - 将数据从 ViewController 传递到实例化的自定义 View Controller

iphone - 如何在 UITextField 中插入蓝色文本标记?

swift - 黄色警告 : Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds

Swift - 使用 UIKeyboardWillChangeFrame 时关闭键盘并调用 TouchUpInside 按钮同时不起作用

python - xcode 可以附加和调试从 c 应用程序运行的 python 吗?

iOS核心剧情: Multiple labels for x-axis

ios - 连续关闭 2 个 ViewController