Search code examples
objective-cipadstoryboarduipopovercontroller

Passing NSIndexPath to New View


I am having real troubles passing an NSIndexPath to my new view. This is how the app works:

I have a UIBarButtonItem in my nab br, tap that and you get a popover view, this shows a bunch of stuff. I need to get an NSIndexPath from my main view, to this popover view.

I have a property for the NSIndexPath in my popover view class and the popover transition is connected up in my storyboard.

Then I have this code to pass the index path across views:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"statsPopover"])
    {
        StatsViewController *statsVC = [segue destinationViewController];

        statsVC.selectedIndex = stageSelectionTable.indexPathForSelectedRow;
    }
}

However, while this gets called, the index path isn't actually sent between views. My index path on the popover is always the default, 0,0 row and section.


Solution

  • You say you know from debugging/logging that the method is running, your if statement is triggered, and stageSelectionTable.indexPathForSelectedRow has the value you expect.

    Doing that kind of diagnosis puts you on the right path to solving your issue. Keep at it -- if you test some other things with NSLog or the debugger you should be able to find the problem.

    • Do you know that statsVC is non-nil? If it's nil, your message to set selectedIndex does nothing.

    • Is statsVC the class you expect? If, say, the (table?) view in your popover is embedded in a navigation controller, segue.destinationViewController will point to the nav controller, and you'll need to look into its child view controllers array to find the one you're looking for.

    • Is whatever accessor method your StatsViewController class is using for its selectedIndex property working right? (This shouldn't be a problem if it's a synthesized accessor for a strong/retain property.)

    • In your StatsViewController class, are you trying to work with selectedIndex before it actually gets set? Setting a property in prepareForSegue:sender: and then using it in viewDidLoad should work fine, but using it in awakeFromNib might not, and using it in an init... method definitely won't.