6

I have created the app with following code. Its working fine with iOS7 but it throws the below error when I run with iOS8.

[UINavigationController setGoalName:]: unrecognized selector sent to instance 0x7964e2c0

My firstViewcontroller.m

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

GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
NSLog(@"%@",[NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);
goalsDetailsViewController.goalName = @"Exercise Daily";

}

My GoalDetailsViewController.h

@interface GoalDetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic) NSString *goalName;

Thanks in advance.

Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38
  • Set a breakpoint in `prepareForSegue:sender:` and examine the `destinationViewController`. It is probably not an actual instance of `GoalDetailsViewController`, and thus will not recognize the `setGoalName:` selector. My guess is that iOS 8 is sending you another view controller that you weren't expecting. – ravron Nov 27 '14 at 18:01
  • It would appear that `goalsDetailsViewController` is a UINavigationController. – Hot Licks Nov 27 '14 at 18:15

2 Answers2

11

Seems like your destinationviewcontroller is a subclass of UINAvigationController.

Try this:

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

GoalDetailsViewController *goalsDetailsViewController = [(UINavigationController*)segue.destinationViewController topViewController];
NSLog(@"%@",[NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);
goalsDetailsViewController.goalName = @"Exercise Daily";

}
gasparuff
  • 2,295
  • 29
  • 48
  • YourViewController *Controller = (YourViewController*)[segue.destinationViewController topViewController]; – IKKA Feb 01 '16 at 14:05
3

The easiest way to handle this crash would be to simply make sure that the destinationViewController is of the type you're expecting before you attempt to set a property on it. Something like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
    NSLog(@"%@",[NSString stringWithFormat:@"%@", [[self.arrCategoryTitle objectAtIndex:indexPath.row] objectAtIndex:indexOfCategory]]);

    if ([segue.destinationViewController isKindOfClass:[GoalDetailsViewController class]]) {
        GoalDetailsViewController *goalsDetailsViewController = segue.destinationViewController;
        goalsDetailsViewController.goalName = @"Exercise Daily";
    }
}

This change ensures that the destinationViewController is of kind GoalDetailsViewController before treating it as such.

ravron
  • 11,014
  • 2
  • 39
  • 66