为什么我的代码执行双重转场?
Why does my code perform a double segue?
我有一个视图控制器 subMenuViewController,它通过两个不同的 segue 链接,称为 DBSubLegislationSegue 和 TrafficSegue。
当用户从动态 table 中选择一个单元格时,代码应检查选择了哪一行,并根据单元格执行正确的转场到新的 viewController。
当我 运行 代码时,应用程序总是在正确的视图控制器上结束,但是它通过执行两个 segue 来做到这一点,第一个 segue 始终是 Traffic Segue。
任何人都可以帮助确定执行此双重转场的原因吗?
//populates the table with instances of SubMenuViewCell for each instance filling its label with the correct menuItems
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
self.tableView.dequeueReusableCellWithIdentifier(
"SubMenuCell", forIndexPath: indexPath)
as! SubMenuTableViewCell
let row = indexPath.row
cell.subMenuLabel.font =
UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.subMenuLabel.text = subMenuItems[row]
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator //adds disclosure arrow indicating further info to cell
return cell
}
//function for handling when a table row is selected by the user.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//get the selected row index and save as rowSelected
let myIndexPath = self.tableView.indexPathForSelectedRow()
rowSelected = myIndexPath!.row
println(rowSelected)
if(rowSelected == 0){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 1){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 2){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 3){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 4){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 5){
performSegueWithIdentifier("TrafficSegue", sender: nil)
}
if(rowSelected == 6){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 7){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 8){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 9){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 10){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 11){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println(segue.identifier)
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "All Entries"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Common Law"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 2 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Police Procedure"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 3 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Police Powers"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 4 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Crime"
}
if segue.identifier == "TrafficSegue" && rowSelected == 5 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! TrafficSubMenuTableViewController
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 6 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "People"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 7 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Civil Order"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 8 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Sexual"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 9 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Licencing"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 10 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Firearms"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 11 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Terrorism"
}
}
您将获得双重转场,因为您通过在情节提要板上按住 Control 并拖动而创建的转场会自动被调用。换句话说,由于您已经通过故事板创建了转场,因此没有理由调用 performSegueWithIdentifier
。假设您已在情节提要中正确设置标识符,其余代码将按原样正常工作,因此您可以继续删除或注释掉 tableview.didSelectRowAtIndexPath
方法的内容。
我建议在准备 segue 时使用 else-ifs,例如:
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 {
//do things
}
else if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 {
// do other things
}
//and so on
全部修复。
我的错误是第一个 segue,我将动态 CELL 连接到下一个视图控制器,但是我的第二个 segue 是从 table 视图本身连接到视图控制器。
通过确保两者都从 table 视图本身连接,代码按预期工作。
我有一个视图控制器 subMenuViewController,它通过两个不同的 segue 链接,称为 DBSubLegislationSegue 和 TrafficSegue。
当用户从动态 table 中选择一个单元格时,代码应检查选择了哪一行,并根据单元格执行正确的转场到新的 viewController。
当我 运行 代码时,应用程序总是在正确的视图控制器上结束,但是它通过执行两个 segue 来做到这一点,第一个 segue 始终是 Traffic Segue。
任何人都可以帮助确定执行此双重转场的原因吗?
//populates the table with instances of SubMenuViewCell for each instance filling its label with the correct menuItems
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
self.tableView.dequeueReusableCellWithIdentifier(
"SubMenuCell", forIndexPath: indexPath)
as! SubMenuTableViewCell
let row = indexPath.row
cell.subMenuLabel.font =
UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.subMenuLabel.text = subMenuItems[row]
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator //adds disclosure arrow indicating further info to cell
return cell
}
//function for handling when a table row is selected by the user.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//get the selected row index and save as rowSelected
let myIndexPath = self.tableView.indexPathForSelectedRow()
rowSelected = myIndexPath!.row
println(rowSelected)
if(rowSelected == 0){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 1){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 2){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 3){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 4){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 5){
performSegueWithIdentifier("TrafficSegue", sender: nil)
}
if(rowSelected == 6){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 7){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 8){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 9){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 10){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
if(rowSelected == 11){
performSegueWithIdentifier("DBSubLegislationSegue", sender: nil)
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
println(segue.identifier)
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "All Entries"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Common Law"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 2 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Police Procedure"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 3 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Police Powers"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 4 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Crime"
}
if segue.identifier == "TrafficSegue" && rowSelected == 5 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! TrafficSubMenuTableViewController
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 6 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "People"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 7 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Civil Order"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 8 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Sexual"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 9 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Licencing"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 10 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Firearms"
}
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 11 { //check the row and act appropriately
let newViewController = segue.destinationViewController //set the new viewController(new page)
as! LegislationDBQueryTableViewController
newViewController.typeOfPage = "Terrorism"
}
}
您将获得双重转场,因为您通过在情节提要板上按住 Control 并拖动而创建的转场会自动被调用。换句话说,由于您已经通过故事板创建了转场,因此没有理由调用 performSegueWithIdentifier
。假设您已在情节提要中正确设置标识符,其余代码将按原样正常工作,因此您可以继续删除或注释掉 tableview.didSelectRowAtIndexPath
方法的内容。
我建议在准备 segue 时使用 else-ifs,例如:
if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 {
//do things
}
else if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 {
// do other things
}
//and so on
全部修复。
我的错误是第一个 segue,我将动态 CELL 连接到下一个视图控制器,但是我的第二个 segue 是从 table 视图本身连接到视图控制器。
通过确保两者都从 table 视图本身连接,代码按预期工作。