Wednesday 19 November 2014

[iOS App] 'NSInvalidArgumentException', reason : unrecognized selector sent to Instance

I am going through iOS App development recently. I use modal segue to move from parent ViewController to child ViewController, as shown in the picture below.


When i return from child ViewController to parent ViewController.  There is an error.


2014-11-20 09:00:48.229 Heritage[64395:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZContinentTableViewController topViewController]: unrecognized selector sent to instance 0x8c60050'

The error shows up when i have 3 level of ViewController, and specifically, from second level ViewController to root ViewController.

I found out the root cause. It is because the unwind segue

- (IBAction)unwindToRegion:(UIStoryboardSegue *)segue
{  }

is mixed up with prepareForSegue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ....
}
   
To fix the error, in prepareForSegue, add the segue identifier checking. Make sure prepareForSegue is only processing the forward segue (In our case, the UserShowSegueCountry).

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    if ([[segue identifier] isEqualToString:@"UserShowSegueCountry"])
    {
    UINavigationController *nav = segue.destinationViewController;
    XYZCountryTableViewController *vcToPushTo = (XYZCountryTableViewController *)nav.topViewController;
        
    }
    

}


1 comment:

  1. In this blog post, you have discussed the common causes of this exception, such as passing invalid arguments to methods or accessing null objects. They also provide insights into debugging techniques and offer suggestions for handling and preventing such exceptions in your iOS app. It's a valuable resource for iOS developers facing NSInvalidArgumentException and seeking solutions to resolve or prevent it in their code. Thanks for sharing this great article, You can also contact us here if you are looking forward to Hire Best Android App Developers from India, and we will be happy to help you.

    ReplyDelete