我使用以下代码创建了应用。它与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;
提前致谢。
答案 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}}。