[UINavigationController setGoalName:]:无法识别的选择器发送到实例0x7964e2c0

时间:2014-11-27 14:27:50

标签: iphone ios7 ios8

我使用以下代码创建了应用。它与iOS7配合使用,但是当我使用iOS8运行时它会抛出以下错误。

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

我的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";

}

我的GoalDetailsViewController.h

@interface GoalDetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic) NSString *goalName;

提前致谢。

2 个答案:

答案 0 :(得分:10)

好像你的destinationviewcontroller是UINAvigationController的子类。

试试这个:

- (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";

}

答案 1 :(得分:3)

处理此崩溃的最简单方法是在尝试在其上设置属性之前,确保destinationViewController属于您期望的类型。像这样:

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

此更改可确保destinationViewController在处理GoalDetailsViewController之前属于{{1}}。