我正在使用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
AND YES我选择了DetailViewController2作为故事板中的自定义类!!!
错误消息:'NSInvalidArgumentException',原因:' - [UINavigationController setDetailArray:]:无法识别的选择器发送到实例0x7fa65a537f00'
答案 0 :(得分:6)
似乎这一行
DetailViewController2 *detailViewController = [segue destinationViewController];
返回UINavigationController
而不是DetailViewController2
如果DetailViewController2
中嵌入了UINavigationController
您可能想要执行以下操作:
UINavigationController *navController = [segue destinationViewController];
DetailViewController2 *detailViewController = (DetailViewController2 *)navController.topViewController;
如果DetailViewController2
是UINavigationController
的子类,您可能忘记在故事板中明确说出它。
答案 1 :(得分:1)
KIDdAe
完全正确DetailViewController2 *detailViewController = [segue destinationViewController];
返回UINavigationController
而不是DetailViewController2
我的答案更多的是清理现有代码,我也会添加答案。
<强> GamesInfoViewController.m 强>
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
// [segue returns a UINavigationController and the controller you
// want is embedded in the navigation controller
UINavigationController *navigationController = [segue destinationViewController];
// We want the top view controller from the navigationController
// and don't forget the cast
DetailViewController2 *detailViewController = (DetailViewController2 *)navigationController.topViewController;
// Removed redundant line as there was no need to create local variable for
// one time use
NSUInteger row = [[self.tableView indexPathForSelectedRow] row];
// There was no need to have these two on on separate lines
GameInfo *gameInfoObject = [GamesInfoArray objectAtIndex:row];
// To add a fail safe you could wrap this in respondsToSelector
// just in the event that the topViewController isn't a DetailViewController2
if ([detailViewController respondsToSelector:@selector(setDetailArray:)]) {
// Note the lowercase for detailArray see comments above this property.
detailViewController.detailArray = @[gameInfoObject.HomeTeam, gameInfoObject.AwayTeam];
}
}
}
<强> DetailViewController2.h 强>
#import <UIKit/UIKit.h>
@interface DetailViewController2 : UITableViewController
// Properties and ivars start with lowercases classes have uppercases
// try to stay to coding conventions it will help you and others when needing help
@property (strong, nonatomic) NSArray *detailArray;
@end
<强> 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;
}
如果您在答案中阅读了评论,您将会看到为什么我改变了一些东西。如果您有任何疑问,请询问。