数据未通过 prepareForSegue 从主 VC 传递到嵌入式 VC
Data not passing from main VC to embedded VC via prepareForSegue
我正在尝试将数据从主 VC(称为 StartVC)传递到嵌入式 VC(称为 MPList - 它本身包含一个表视图)。
我找到的解决方案都指向使用 prepareforsegue 方法,但这似乎对我不起作用。该应用程序运行没有错误,并且表格视图没有填充我发送的数据。
在StartVC中,我实现了prepareforsegue方法如下(在attributes inspector中,embed segue肯定有一个标识符"LeftPane"):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "LeftPane"{
let mpListVC = segue.destination as? MPList
mpListVC?.mpNames = self.mpNames
}
}
mpNames 是在嵌入式 MPList 中初始化的字符串数组,如下所示(mpListTV 是相关表视图的 IBOUTLET):
class MPList: UIViewController, UITableViewDataSource, UITableViewDelegate{
var mpNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
mpListTV.delegate = self
mpListTV.dataSource = self
print("names in MPList is: \(mpNames)")
}
常用的表视图控制方法也在 MPList 中实现(这也是使用 mpNames 的地方。):
//MARK: - TABLEVIEW METHODS
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return mpNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = mpNames[indexPath.row]
return cell
}
我还确保右键单击并从 tableview 拖动到 MPList 顶部的黄色小图标 viewcontroller 并设置数据源和委托。
在这几个小时后,我可能只是累了(并且遗漏了一些非常明显的东西),但是有人可以让我摆脱痛苦并告诉我我错了什么吗?
我在这里看到的可能问题:
1.
//in prepareForSegue
let mpListVC = segue.destination as? MPList
确保 IB 中的目标视图控制器确实 设置为 MPList
。也许 mpListVC
就是 nil
的原因。只需调试并确保 mpListVc
不为零。
同时进行调试并确保没有可选项为零。最好使用强制展开 (!
) 尝试在某处找到可能的 nil
。
只需设置一个断点并检查是否确实设置了mpNames
。
2.
viewDidLoad
of MPList
可能由于某种原因在 prepareForSegue
之前被调用,尽管它不应该被调用。无论哪种方式,重置数组后 UITableView
的 reloadData
都是一个好习惯。
var mpNames = [String]() {
didSet {
mpListView?.reloadData()
}
}
我正在尝试将数据从主 VC(称为 StartVC)传递到嵌入式 VC(称为 MPList - 它本身包含一个表视图)。
我找到的解决方案都指向使用 prepareforsegue 方法,但这似乎对我不起作用。该应用程序运行没有错误,并且表格视图没有填充我发送的数据。
在StartVC中,我实现了prepareforsegue方法如下(在attributes inspector中,embed segue肯定有一个标识符"LeftPane"):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "LeftPane"{
let mpListVC = segue.destination as? MPList
mpListVC?.mpNames = self.mpNames
}
}
mpNames 是在嵌入式 MPList 中初始化的字符串数组,如下所示(mpListTV 是相关表视图的 IBOUTLET):
class MPList: UIViewController, UITableViewDataSource, UITableViewDelegate{
var mpNames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
mpListTV.delegate = self
mpListTV.dataSource = self
print("names in MPList is: \(mpNames)")
}
常用的表视图控制方法也在 MPList 中实现(这也是使用 mpNames 的地方。):
//MARK: - TABLEVIEW METHODS
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return mpNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = mpNames[indexPath.row]
return cell
}
我还确保右键单击并从 tableview 拖动到 MPList 顶部的黄色小图标 viewcontroller 并设置数据源和委托。
在这几个小时后,我可能只是累了(并且遗漏了一些非常明显的东西),但是有人可以让我摆脱痛苦并告诉我我错了什么吗?
我在这里看到的可能问题:
1.
//in prepareForSegue
let mpListVC = segue.destination as? MPList
确保 IB 中的目标视图控制器确实 设置为 MPList
。也许 mpListVC
就是 nil
的原因。只需调试并确保 mpListVc
不为零。
同时进行调试并确保没有可选项为零。最好使用强制展开 (!
) 尝试在某处找到可能的 nil
。
只需设置一个断点并检查是否确实设置了mpNames
。
2.
viewDidLoad
of MPList
可能由于某种原因在 prepareForSegue
之前被调用,尽管它不应该被调用。无论哪种方式,重置数组后 UITableView
的 reloadData
都是一个好习惯。
var mpNames = [String]() {
didSet {
mpListView?.reloadData()
}
}