【发布时间】:2017-08-28 18:00:51
【问题描述】:
我有第一个视图控制器(FirstView Controller),它显示了两个 TextFields 和一个 TableView,其中包括 TableViewCell 和 Label。而且我有第二个视图控制器(TypeViewController),它还包括 TableView 和包含一个标签的 TableViewCell。
在 FirstViewController 上选择带有文本的 TableViewCell Choose -> 继续查看 TypeViewController
在 TypeViewController 上选择 TableViewCell(在 TypeViewController 上,我有单元格列表,可以动态加载)-> 返回 FirstViewController,而不是文本 Choose 显示在 TypeViewController 上选择的文本。
如何将 FirstViewController 上 TableViewCell 上的 Label 文本更改为 TypeViewController 中的文本?
第一个视图控制器
import UIKit
class FirstViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var animalTypeName: String?;
var selectedTypeId: UInt?;
var selectedTypeName: String?;
override func viewDidLoad() {
super.viewDidLoad();
}
// MARK: TableView && TableViewCell Methods
// Row display.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FirstTableViewCell;
animalTypeName = "Choose";
cell.typeName.text = animalTypeName;
return cell;
}
//MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "ShowType":
guard segue.destination is TypeViewController else {
fatalError("Unexpected destination: \(segue.destination)");
}
default:
fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))");
}
}
//MARK: Actions
@IBAction func unwindToCreateMedicamentController(sender: UIStoryboardSegue) {
if let typeViewController = sender.source as? TypeViewController, let type = typeViewController.type {
selectedTypeName = type.name;
selectedTypeId = type.animalTypeId;
animalTypeName = selectedTypeName;
}
}
}
TypeViewController
import UIKit
class TypeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var typeList: [AnimalType]?;
var type: AnimalType?;
let typeController = AnimalTypeController();
override func viewDidLoad() {
super.viewDidLoad();
// Call method to get array of AnimalType items.
getArrayOfTypeItems();
}
//MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
// Row display.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return typeList?.count ?? 0;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "TypeTVCell";
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TypeTableViewCell else {
fatalError("The dequeued cell is not an instance of TypeTVCell.");
}
let typeItem = typeList?[indexPath.row];
cell.nameOfType.text = typeItem!.name;
return cell;
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "ChooseType":
guard segue.destination is CreateMedicamentController else {
fatalError("Unexpected destination: \(segue.destination)");
}
guard let selectedTypeCell = sender as? TypeTableViewCell else {
fatalError("Unexpected sender: \(String(describing: sender))");
}
guard let indexPath = tableView.indexPath(for: selectedTypeCell) else {
fatalError("The selected cell is not being displayed by the table");
}
let selectedType = typeList?[indexPath.row];
type = selectedType;
default:
fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
}
}
}
【问题讨论】:
-
你需要设置委托。
-
这一定是 SO 上最常见的 iOS 问题之一。 medium.com/@jamesrochabrun/…
标签: ios swift uitableview swift3 segue