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

I created an application with the following code. It works great with iOS7, but when launching iOS8, the error below appears.

[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.

+5
source share
2 answers

It seems your destinationviewcontroller is a subclass of UINAvigationController.

Try the following:

 - (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"; } 
+10
source

The easiest way to deal with this failure is to simply make sure that destinationViewController is of the type you expect before trying to set a property on it. Something like that:

 - (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 destinationViewController looks like GoalDetailsViewController before considering it as such.

+3
source

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


All Articles