2015-09-09 2 views
0

У меня есть контроллер подменю subMenuViewController, который соединяется через два разных сегмента, называемых DBSubLegislationSegue и TrafficSegue.Почему мой код выполняет двойной segue?

Когда пользователь выбирает ячейку из динамической таблицы, код должен проверять, какая строка была выбрана, и в зависимости от ячейки будет выполнять правильный переход к новому диспетчеру viewController.

Когда я запускаю код, приложение всегда заканчивается на правильном контроллере просмотра, но это делает это, выполняя два сегмента, причем первый сеанс всегда является 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" 
    } 

} 
+0

В вашем раскадровке вы ctrl-dragged, чтобы создать segue для нового контроллера представления? –

+0

У меня есть элемент управления, который перетаскивается из динамической ячейки, чтобы отображать контроллеры с разными segues для каждого. – Aldo

ответ

0

Вы получаете двойные сборы, потому что вызовы, созданные вами, управляются перетаскиванием на раскадровку, автоматически вызываются. Другими словами, поскольку вы создали segues через раскадровку, нет причин звонить performSegueWithIdentifier. Предполагая, что вы правильно установили идентификаторы в раскадровке, остальная часть вашего кода будет работать нормально, как есть, поэтому вы можете просто продолжить или удалить содержимое вашего метода tableview.didSelectRowAtIndexPath.

я предлагаю использовать либо еще-сослагательное наклонение в вашей подготовке к Segue, как:

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

Отличное спасибо Beanstock – Aldo

0

Все фиксирован.

Моя ошибка была с первым segue, который я подключил динамический CELL к следующему контроллеру представления, однако мой второй сеанс был подключен из самого представления таблицы к контроллеру представления.

Убедившись, что оба из них были связаны с табличным представлением, код работает так, как ожидалось.

Смежные вопросы