Xcode 6 - Click on the same controller

I have a table view controller, and clicking on a cell can trigger one of many different types of push segments based on the data type in the cell. The identifier of the correct segment is defined in the table View: didSelectRowAtIndexPath: and then segue starts with self.performSegueWithIdentifier:

For one of these sessions, I would like to push another instance of the same view controller onto the navigation stack. Is this possible without dragging a new controller object of the same type onto the storyboard? So far, I have managed to connect the session to one view controller if I Ctrl + Drag from a table cell to the view controller. This is not what I want.

+6
source share
3 answers

As suggested, I don't think segue will work. Instead, you can program a new instance of the same view controller, which will have the same effect as executing segue. The difference is that segue delegate methods like prepareForSegue will not be called.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MyViewController *vc = [[MyViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES]; } 
+10
source

Having completed the answer above, you can do this in Swift 3 using:

  let vc = ViewController() self.navigationController?.show(vc, sender: nil) 

If you are using a storyboard, you can use this:

 let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! YourViewController self.navigationController?.show(vc, sender: nil) 
0
source
  1. Create a layout in IB.
  2. Refer to the view controller.
  3. Then you can create a segue from the view controller into a link that refers to itself.
0
source

Source: https://habr.com/ru/post/977141/


All Articles