ios - 如何为多个屏幕的进程重用 View Controller ?

标签 ios swift

我正在开发一种聊天应用程序,它会提出一些问题,用户可以在其中回答。

每个问题都有自己的屏幕,在用户回答问题后,会有一个导航到下一个屏幕的按钮。

所有的屏幕几乎都不一样。一个 UILabel 和输入字段。现在我想知道是否有一种方法可以重用这个 View ,这样我就不必创建多个完全相同的 View Controller 。

这可能吗?我应该寻找什么?

最佳答案

您可以通过以下方式执行此操作:

  1. 在重用 View Controller 中创建变量:

    var question: String!
    
  2. 按钮 Action 需要执行转场:

    @IBAction func nextQuestionAction() {
        performSegue(withIdentifier: "NextQuestionSegue", sender: nil)
    }
    
  3. 在为 segue 做准备时,在转换之前,您分配下一个问题:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        if segue.identifier == "NextQuestionSegue" {
            guard let destinationVC = segue.destination as? ReuseViewController else { return }
            destinationVC.question = "Question goes here"
        }
    }
    

这就是它的工作方式。

关于ios - 如何为多个屏幕的进程重用 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47071027/

相关文章:

javascript - 使用 Objective-c 捕获 JavaScript 异步调用

ios - Metal (iOS) 中的多重采样/锯齿状边缘

ios - 如果 IBOutlet 在 UIViewController 类中,如何访问 UITableViewController 类中的 IBOutlet

ios - 在 Swift 中调用 CGPatternCreate

swift - 转动手腕时流媒体暂停 - watchOS/Swift

ios - Bluefruit LE 连接登录问题

ios - "Expression was too complex"从数组字面量初始化 Set

swift - 如何在使用 Swift 3 中的 Measurements 的函数调用中使用泛型

ios - UICollectionView reloadData 索引超出范围

ios - UIButton颜色在iphone 8 plus和iphone X中有渐变(iOS版本独立问题)