[英]How to change data one a view controller based on the cell tapped
I am doing an assignment, In ViewController A, I have three cells, Calcium, Alkalinity, Magnesium. 我正在做一个分配,在ViewController A中,我有三个单元格:钙,碱度,镁。 I need to update ViewController B based on the cell tapped in ViewController A. ie if Alkalinity was tapped, the label on ViewController B should say "Alkalinity", if calcium is tapped, ViewController 2 should say calcium etc.
我需要基于在ViewController A中点击的单元格来更新ViewControllerB。即,如果点击了碱度,则ViewController B上的标签应显示为“碱度”,如果点击了钙,则ViewController 2应显示钙等。
Should I create a segue for each cell? 我应该为每个单元创建一个序列吗? Or can I do this via code?
或者我可以通过代码做到这一点?
I tried using a segue but I feel as if it is to many segues... Im thinking to use cell tags? 我尝试使用segue,但感觉好像对许多segue一样...我想使用细胞标记吗?
no error yet 尚无错误
Option without segues: 没有选择的选项:
1) In interfaz builder, assign a identifier to ViewControllerB
1)在interfaz构建器中,为
ViewControllerB
分配一个标识符
2) ViewControllerA
need conform UITableViewDelegate
protocol (or UICollectionViewDelegate
if you use UICollectionView
) 2)
ViewControllerA
需要符合UITableViewDelegate
协议(如果使用UICollectionView
则需要UICollectionViewDelegate
协议)
3) Implement the method tableView(_:didSelectRowAt:)
in ViewControllerA
3)在
ViewControllerA
实现方法tableView(_:didSelectRowAt:)
4) In the method, instantiate a ViewControllerB
instance, example: 4)在方法中,实例化一个
ViewControllerB
实例,例如:
let vc = UIStoryboard(name: "nameofthestoryboard").instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
5) Get the cell selected with the index path. 5)获取带有索引路径的选定单元格。
let cell = tableview.cellForRow(at: indexPath) as! YourCellType
6) Setup the necessary information to viewControllerB, example: 6)设置必要的信息到viewControllerB,例如:
vc.chemicalElement = cell.chemicalElement
7) Present the ViewControllerB instance that you created. 7)显示您创建的ViewControllerB实例。
present(vc, animated: true, completion: nil)
And that is all. 仅此而已。
If you are using a simple segue to move between view controllers than to pass data from one to the other you can do something like this. 如果您使用一种简单的方法在视图控制器之间移动,而不是将数据从一个传递到另一个,则可以执行以下操作。
You need to instantiate the title variable in ViewControllerA. 您需要在ViewControllerA中实例化title变量。
var elementName : String?
Using the function prepare(for segue: UIStoryboardSegue, sender: Any?)
使用函数
prepare(for segue: UIStoryboardSegue, sender: Any?)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "SEGUE IDENTIFIER HERE") {
var vc = segue.destinationViewController as ViewControllerB
vc.title = elementName
}
}
You also need to instantiate the title variable in ViewControllerB. 您还需要在ViewControllerB中实例化title变量。
var title : String?
If you post your code I can give you more specific help otherwise this is the flow you could use to accomplish this task 如果您发布代码,我可以为您提供更多具体的帮助,否则这就是您可以用来完成此任务的流程
You can create a public variable on viewController B, 您可以在viewController B上创建一个公共变量,
public var componentName : String?
then pass the selected value(from did select method) from ViewController A. 然后从ViewController A传递选择的值(来自select方法)。
let ViewB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewcontrollerB")
ViewB.componentName = componentName //get this component name from index path
self.navigationController?.show(ViewB, sender: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.