Search code examples
iosdelegatestableviewdidselectrowatindexpath

call view controller segue from within tableView delegate class


I have separated my tableView delegate and data source from the tableView controller. so far its working great. however, I have now come to a place where I need to implement the didSelectRowAtIndexPath delegate method. When this method is called I need to perform a segue from my view controller, but I dont have a reference to the view controller from inside the delegate class to call the method like [myViewController performSegueWithIdentifier...

Whats the proper way to handle this? Do I just add a property to my delegate to store the view controller? That does not seem like a good idea since the view controller has a reference to the delegate already. How can I reference back the VC? I was thinking I could post a notification but there has to be a better way than that. Ive always just used the view controller as the delegate so I have never had to deal with this before.


Solution

  • This question seems strange to me because if you create the segue in storyboard, say a push segue from a tableView cell to another viewcontroller, then when the tableview cell is tapped the segue is automatically triggered. You can do other things in

    - (void)prepareForSegue:(UIStoryboardSegue *)segue
                     sender:(id)sender
    

    as for

    add a property to my delegate to store the view controller

    It's ok. Because

    @property(nonatomic, assign) id< UITableViewDelegate > delegate
    

    viewcontroller doesn't retain the delegate