【发布时间】:2017-04-19 16:43:44
【问题描述】:
我对 Swift 比较陌生 - 为我的医院(我是一名住院医师)开发一个应用程序,它本质上是一个移动参考工具,我正在使用一系列 tableViews 构建它。
问题:我无法让我的 Segues 从我的原型单元到多个 VC 工作。
- 我的初始 VC 是一个 tableVC,我使用嵌套数组将原型单元构建成两个部分。 Seen here
- 目标 VC 也是 TableVC,我希望初始 VC 的每个 tableCell 都连接到不同的目标 TableVC。
- 我能够设置一些从初始TVC 到destinationTVC 的Segue,我还嵌入了一个导航控制器--> Here's what my storyboard setup looks like
我想为 Segues 做些什么:
设置 segue ID,我使用 prepareForSegue 函数在情节提要中为每个 segue 提供
使用 didSelectRowAtIndexPath 和一些 If 语句指定我要执行哪个单元格,执行哪个 segue。
-
保留 Storyboard Navigation 控制器 - 喜欢它提供的功能和简单的实现,考虑到我将在进入我的 detailView 页面之前将这些 segue 的多级添加为“子菜单”。
这是我的 Swift 3 代码:
// MainTableViewController.swift
import UIKit
class MainTableViewController: UITableViewController {
var MainTitleArray = [["Children and Pregnant Women", "Mental Health and Addiction", "Hunger and Food Insecurity", "Legal Aid", "Housing, Heat, and Emergency Assistance", "New Immigrants, Refugees, and the Underserved", "The Uninsured and the Underinsured"], ["Primers and the Private Sector", "Federal Programs", "Social Securty"]]
var SubTitleArray = [["Resources for the most vulnerable in the family", "The programs to help those who need it", "When every night is a worry about food on the table", "How to defend yourself in a complicated system", "HEalth follows you into the home", "Your patient needs help - can they sign up worry-free?", "What to do when your options are limited"], ["What you need to know","Medicare Medicaid - What are they?","Disability and Insurance"]]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 90
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return MainTitleArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MainTitleArray[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainCell", for: indexPath) as! MainTableViewCell
cell.MainTitleLabel.text = MainTitleArray[indexPath.section][indexPath.row]
cell.MainSubTitleLabel.text = SubTitleArray[indexPath.section][indexPath.row]
return cell}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.section == 0) && (indexPath.row == 0) {
self.performSegue(withIdentifier: "CPWSegue", sender: indexPath);
}
else { self.performSegue(withIdentifier: "MHASegue", sender: indexPath)
}}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CPWSegue" {
let destVC = segue.destination as! CPWTableViewController
destVC.VCName = "CPW"}
}
}
我成功地构建了它,但 segues 仍然无法正常工作。我几乎宁愿仍然有一些错误代码来处理一些事情!如果有人有任何有用的建议/cmets/链接以供进一步阅读,非常将不胜感激。已经阅读了大量的其他页面和文档,试图让它像我想要的那样简单地工作,但到目前为止还没有骰子。提前感谢您提供的任何帮助!
2017 年 4 月 20 日更新:我已经编辑了 prepareForSegue 函数,甚至在其中放入了一个“VCName”变量,这样我就可以调用 segue.destination(即使“VCName”是 not used)- STILL 没有给我一个 segue(没有错误消息,并且它成功构建)并且不确定接下来要尝试什么。任何建议表示赞赏!
2017 年 4 月 26 日更新:我使用了一些断点,发现 prepare 和 perform segue 函数都没有触发。我重建并确保它是超类的 override 函数(不知道为什么它不允许我事先这样做),并且它有效!特别感谢 Sasaang 的帮助!
【问题讨论】:
-
您能否尝试在代码中添加几个断点,一个在 self.preformSegue 调用处,另一个在 prepare for segue 函数中的某个位置,并检查当您点击时是否触发其中一个或两个正确的单元格。
-
尝试了断点,由于某种原因,当我点击任何单元格时,准备和执行功能都不会触发。 - - 也许我需要重新配置我的 didSelectRowAtIndexPath if else 语句的格式?
-
您能否更新您的问题以包含整个班级的代码。如果我能看到整个事情可能会有所帮助。
-
已更新以包含完整代码 - 我是否需要在我的代码中使用 segue 进行一些 IB 引用?
-
好的,你说它没有在 performSegue 调用中达到断点。你能检查一下它是否完全达到了这个功能。在检查节和行的 if 语句之前;添加以下打印语句并将断点设置到这一行: print(indexPath.section, indexPath.row) ,这样你也可以在控制台中看到索引的值是什么。如果功能设置正确,则应始终触发此行。
标签: swift uinavigationcontroller ios6 segue didselectrowatindexpath