[英]How to make a list with Table View Cells with user input from another view controller swift
I'm making an iOS app with Swift that has two scenes. 我正在用Swift开发一个具有两个场景的iOS应用。 One asks for user input and that is saved in an array.
有人要求用户输入,然后将其保存在数组中。 I'm trying to make a list with table view cells in the other scene, and that list includes the user input that is saved when the user clicks on the save button.
我正在尝试创建一个列表,其中包含另一个场景中的表格视图单元格,并且该列表包括当用户单击“保存”按钮时保存的用户输入。
tried creating a sharedData class in a separate file, tried this: 尝试在单独的文件中创建sharedData类,请尝试以下操作:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
var svc = segue!.destinationViewController as! goalViewController
svc.toPass = textInput
}
}
(both from other stackoverflow questions) (均来自其他stackoverflow问题)
尝试将数组保存到NSUserDefaults中,然后在新视图中调用保存在NSUserDefaults中的对象。
- Using Delegate
is one of the preferred way -使用
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: 现在,在要访问数组的类中,执行以下操作:
- Conform to protocol
(Delegate) defined above -符合以上定义的
protocol
(代理)
- Assign self
to the delegate
-将
self
分配给delegate
- Implement the delegate method
and then access the array from inside it. -实现
delegate method
,然后从内部访问数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.