我正在使用xcode中的情节提要,并且在prepareForSegue方法期间崩溃。我想要做的是从表格视图中单击单元格后进入另一个详细的表格视图。这是代码。

GamesInfoViewController.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
    DetailViewController2 *detailViewController = [segue destinationViewController];

    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

    NSUInteger row = [myIndexPath row];
    GameInfo *gameInfoObject;

    gameInfoObject =[GamesInfoArray objectAtIndex:row];
    detailViewController.DetailArray = @[gameInfoObject.HomeTeam, gameInfoObject.AwayTeam];  // IT CRASHES HERE!!!
}

}


这是DetailViewController2.m

- (void)viewDidLoad {
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

[self.tableView reloadData];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GamesInfoTableViewCell *cell = (GamesInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MainCell"];

cell.mainLabel.text = _DetailArray[0];
cell.awayTeamLabel.text = _DetailArray[1];

// Configure the cell...

return cell;


}

DetailViewController2.h

#import <UIKit/UIKit.h>

@interface DetailViewController2 : UITableViewController

@property (strong, nonatomic) NSArray *DetailArray;
@end


是的,我已选择DetailViewController2作为情节提要中的自定义类!

错误消息:“ NSInvalidArgumentException”,原因:“-[UINavigationController setDetailArray:]:无法识别的选择器已发送到实例0x7fa65a537f00”

最佳答案

看来这条线

DetailViewController2 *detailViewController = [segue destinationViewController];


返回UINavigationController而不是DetailViewController2

如果DetailViewController2嵌入在UINavigationController中,则可能需要执行以下操作:

UINavigationController *navController = [segue destinationViewController];
DetailViewController2 *detailViewController = (DetailViewController2  *)navController.topViewController;


如果DetailViewController2UINavigationController的子类,则您可能已经忘记在情节提要中明确地说出来了。

10-08 05:24