我正在尝试在做一些教程后制作我的第一个iOS应用程序。我正在创建一个简单的标题/新闻阅读器,将新奥尔良圣徒的头条和故事作为来自ESPN API的json,在表格视图中显示标题,然后在用户点击标题时在详细视图中显示故事的文本。
应用程序编译并运行时没有错误或警告。当用户点击顶部tableview中的标题时,将执行prepareForSegue方法。我可以在调试器中执行此代码。如果我逐步执行prepareForSegue方法,它会调用detailViewController的setStory方法,该方法又调用configureview方法。但后来它将我带到main.m并关闭应用程序,而不显示详细信息视图。
我是个菜鸟。我错过了什么?为什么我看不到细节视图?也许我需要一个IBAction来加载IBOutlet?
saintsMasterViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
saintsDetailViewController *detailViewController = [segue destinationViewController];
saintsNewsStory* displayThisStory =[self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row];
[detailViewController setStory:displayThisStory];
}
}
saintsDetailViewController.m
- (void)setStory:(saintsNewsStory *)newStory
{
// if (_story != newStory) {
_story = newStory;
// Update the view.
// [self configureView];
// }
}
- (void)configureView
{
self.storyText.text = self.story.storyText;
}
- (void)viewDidLoad //the app never gets here. if i put a breakpoint here it never gets here
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
saintsDetailViewController.h
@class saintsNewsStory;
@interface saintsDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *storyText;
@property (retain, nonatomic) saintsNewsStory *story;
@end
控制台显示以下错误:
SaintsHeadlineReader [9608:11303] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是键值detailDescriptionLabel的键值编码兼容。 * 第一次抛出调用堆栈: (0x1c90012 0x10cde7e 0x1d18fb1 0xb7a711 0xafbec8 0xafb9b7 0xb26428 0x2320cc 0x10e1663 0x1c8b45a 0x230bcf 0xf5e37 0xf6418 0xf6648 0xf6882 0x102235 0x3013d2 0xff4f3 0xff777 0xff7b7 0x46afe2 0x45cad9 0x45cb54 0xc4899 0xc4b3d 0xacbe83 0x1c4f376 0x1c4ee06 0x1c36a82 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c 0x204d 0x1f75为0x1) libc ++ abi.dylib:terminate调用抛出异常
看起来textview有一个细节标签,也许我需要一个出口呢?
答案 0 :(得分:0)
更改此行:
saintsDetailViewController *detailViewController = [segue destinationViewController];
对此:
saintsDetailViewController *detailViewController = (saintsDetailViewController *)[segue destinationViewController];
编辑:
您应首先显示您的视图,然后调用configureView
方法。尝试在该ViewController的configureView
中调用viewDidLoad
。
- (void)setStory:(saintsNewsStory *)newStory
{
// if (_story != newStory) {
_story = newStory;
// Update the view.
// [self configureView];
// }
}
-(void) viewDidLoad {
[self configureView];
}
- (void)configureView
{
self.storyText.text = self.story.storyText; //the app gets to here but does not show the view. why?
}
答案 1 :(得分:0)
测试我的理论。在您当前使用NSLog(@"%d", [self.yourTableView indexPathForSelectedRow].row);
的行之前或之后立即将prepareForSegue
放入当前indexPathForSelectedRow
方法中,并查看它在您的控制台中返回的内容。可能是0。
不是将segue从UITableViewCell Prototype连接到DetailViewController,而是将它从UITableViewController连接到DetailViewController,然后使用以下方法:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SaintsDetailViewController *detailViewController = [[SaintsDetailViewController alloc] init];
saintsNewsStory* displayThisStory =[self.dataController objectAtIndex:indexPath.row];
[detailViewController setStory:displayThisStory];
[self performSegueWithIdentifier:@"showDetail" sender:indexPath];
}
和您的prepareForSegue:
方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
saintsDetailViewController *detailViewController = [segue destinationViewController];
}
}