首页 文章

如何使用来自另一个视图控制器swift的用户输入创建具有表视图单元格的列表

提问于
浏览
0

我正在使用Swift制作一个有两个场景的iOS应用程序 . 一个请求用户输入并保存在一个数组中 . 我正在尝试在另一个场景中创建一个包含表格视图单元格的列表,该列表包含用户单击保存按钮时保存的用户输入 .

尝试在单独的文件中创建一个sharedData类,试过这个:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "segueTest") {
        var svc = segue!.destinationViewController as! goalViewController
        svc.toPass = textInput
    }
}

(均来自其他stackoverflow问题)

2 回答

  • 0

    尝试将阵列保存到NSUserDefaults中,然后在新视图中调用您在NSUserDefaults中保存的对象 .

  • 0

    - 使用 Delegate 是首选方式之一

    Example:

    protocol PExposeArrayDelegate: class
    {
       func getArrayOfItems(array: String)
    }
    
    
    class CustomArray
    {
    
    // delegate property to expose array to the view-controller
        internal weak var delegateForTimeLineCell: PExposeArrayDelegate?
    
    
    internal func createArray()
    {
        let arr = ["Vivek","India","Audi"]
    
         delegateForTimeLineCell.getArrayOfItems(array: arr)
    }
    
    }
    

    Now in the class where you want to access the array, do the following:

    - 符合上面定义的 protocol (代表)

    -self 分配给 delegate

    - 实现 delegate method 然后从其中访问数组 .

    Check the example here

相关问题