0

在我的应用程序中,我有 NavigationController 作为 root,我也有一个导航按钮。当我按下按钮时,它会翻转到另一个控制器。

目标视图代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"favorites.plist"];
    NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

    self.title = [unArchiver decodeObjectForKey:@"title"];
    self.pubDate = [unArchiver decodeObjectForKey:@"pubdate"];
    self.description = [ unArchiver decodeObjectForKey:@"description"];
}

按钮代码1:

  -(void)navFavoritesButton{
    [UIView beginAnimations:@"flipview" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:self.view cache:YES];
    FavoritesView *fav = [[FavoritesView alloc]initWithStyle:UITableViewStylePlain];
    UINavigationController *favNav = [[UINavigationController alloc]initWithRootViewController:fav];
    favNav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:favNav animated:YES];
}

按钮代码2:

-(void)navFavoritesButton{
    [UIView beginAnimations:@"flipview" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:self.view cache:YES];
    FavoritesView *fav = [[FavoritesView alloc]initWithStyle:UITableViewStylePlain];
    fav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:fav animated:YES];

}

我有一个奇怪的问题;如果我使用按钮 2 代码一切正常,则加载视图,取消归档数据并将其显示在 tableView 上。

但是,如果我使用按钮 1 代码,则应用程序崩溃并显示以下日志:

 2013-04-21 16:51:56.714 YnetXML[21898:f803] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x6b4fd30
2013-04-21 16:51:56.715 YnetXML[21898:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x6b4fd30'
*** First throw call stack:
(0x268c052 0x1a61d0a 0x268dced 0x25f2f00 0x25f2ce2 0xc312d1 0xc9830e 0x82123 0xc9764e 0xc96c1c 0xcbd56d 0xca7d47 0xcbe441 0xcbe4f9 0xeb5c68 0xc754a1 0xc7612b 0xeb54c7 0xc9e427 0xc9e58c 0x348c4 0xc9e5cc 0x34568 0x387d 0x268dec9 0xbd45c2 0xe0fd54 0x268dec9 0xbd45c2 0xbd455a 0xc79b76 0xc7a03f 0xc792fe 0xbf9a30 0xbf9c56 0xbe0384 0xbd3aa9 0x35a9fa9 0x26601c5 0x25c5022 0x25c390a 0x25c2db4 0x25c2ccb 0x35a8879 0x35a893e 0xbd1a9b 0x22ed 0x2215)
terminate called throwing an exception(lldb)

编辑:

我删除了下面的行,它适用于按钮 1 和 2 代码。什么应该导致问题?

self.title = [unArchiver decodeObjectForKey:@"title"];

可能是什么问题呢?为什么它在没有 navigationController 的情况下运行良好并崩溃?

我试图解决这个问题几个小时,我尝试了各种方法,我在网上阅读但没有任何效果。我只是不知道是什么原因导致它崩溃,请帮忙!

4

1 回答 1

0

我想问题出在这三个陈述上

self.title = [unArchiver decodeObjectForKey:@"title"];
self.pubDate = [unArchiver decodeObjectForKey:@"pubdate"];
self.description = [ unArchiver decodeObjectForKey:@"description"];

尝试这个:

//I guess problem with this, because only when you bring in a navigation controller , your app crashes so only when navigation controller comes into play the navigation bar appears and self.title is the text you see in the center of the navigation bar - so rename self.title to self.titlesArray or something

if([[unArchiver decodeObjectForKey:@"title"] isKindOfClass:[NSString class]])
{
 //do not use self.title for other purposes, it is to set title of navigation bar.
self.title = [unArchiver decodeObjectForKey:@"title"];
}

self.pubDate = [unArchiver decodeObjectForKey:@"pubDate"];
self.description= [unArchiver decodeObjectForKey:@"description"];

注意:这不是实现的方法,但这只是一种找出这些语句是否是导致崩溃的原因的方法。试试这个并告诉我,我们会想出另一种方法。

于 2013-04-21T16:00:35.107 回答