为什么我的代码执行双segue?

时间:2022-06-08 20:21:10

I have on view controller subMenuViewController that links via two different segues called DBSubLegislationSegue and TrafficSegue.

我有视图控制器subMenuViewController,它通过两个不同的段来链接,称为DBSubLegislationSegue和TrafficSegue。

When the user selects a cell from a dynamic table the code should check what row was selected and depending on the cell will perform the correct segue to a new viewController.

当用户从动态表中选择一个单元格时,代码应该检查选择了哪一行,并且根据单元格将执行到新viewController的正确segue。

When I run the code the app always ends up on the correct view controller however it aways does this by performing two segues, with the first segue always being the Traffic Segue.

当我运行代码时,应用程序总是会在正确的视图控制器上结束,但它通过执行两个segue来完成此操作,第一个segue始终是Traffic Segue。

Can anyone assist in identifying why this double segue is performed?

任何人都可以帮助确定为什么要执行这个双重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"
    }

}

2 个解决方案

#1


0  

All fixed.

全部固定。

My error was with the first segue I connected the dynamic CELL to the next view controller however my second segue was connected from the table view itself to the view controller.

我的错误是第一个segue我将动态CELL连接到下一个视图控制器但是我的第二个segue从表视图本身连接到视图控制器。

By ensuring both were connected from the table view itself the code works as expected.

通过确保从表视图本身连接两者,代码按预期工作。

#2


0  

You're getting double segues because the segues you created by control-dragging on the storyboard automatically get called. In other words, since you've created the segues through the storyboard, there's not reason to call performSegueWithIdentifier. Assuming you've set the identifiers correctly in the storyboard, the rest of your code will work just fine as is, so you can just go ahead and delete or comment out the contents of your tableview.didSelectRowAtIndexPath method.

你得到了双重分段,因为通过控制拖动在故事板上创建的segue会自动被调用。换句话说,由于您已经通过故事板创建了segue,因此没有理由调用performSegueWithIdentifier。假设您已在故事板中正确设置了标识符,则其余代码将按原样正常工作,因此您可以继续删除或注释掉tableview.didSelectRowAtIndexPath方法的内容。

I suggest using else-ifs in your prepare for segue, like:

我建议在准备segue时使用else-ifs,例如:

if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 {
    //do things
}
else if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 {
    // do other things
}
//and so on

#1


0  

All fixed.

全部固定。

My error was with the first segue I connected the dynamic CELL to the next view controller however my second segue was connected from the table view itself to the view controller.

我的错误是第一个segue我将动态CELL连接到下一个视图控制器但是我的第二个segue从表视图本身连接到视图控制器。

By ensuring both were connected from the table view itself the code works as expected.

通过确保从表视图本身连接两者,代码按预期工作。

#2


0  

You're getting double segues because the segues you created by control-dragging on the storyboard automatically get called. In other words, since you've created the segues through the storyboard, there's not reason to call performSegueWithIdentifier. Assuming you've set the identifiers correctly in the storyboard, the rest of your code will work just fine as is, so you can just go ahead and delete or comment out the contents of your tableview.didSelectRowAtIndexPath method.

你得到了双重分段,因为通过控制拖动在故事板上创建的segue会自动被调用。换句话说,由于您已经通过故事板创建了segue,因此没有理由调用performSegueWithIdentifier。假设您已在故事板中正确设置了标识符,则其余代码将按原样正常工作,因此您可以继续删除或注释掉tableview.didSelectRowAtIndexPath方法的内容。

I suggest using else-ifs in your prepare for segue, like:

我建议在准备segue时使用else-ifs,例如:

if segue.identifier == "DBSubLegislationSegue" && rowSelected == 0 {
    //do things
}
else if segue.identifier == "DBSubLegislationSegue" && rowSelected == 1 {
    // do other things
}
//and so on