EXC_BAD_ACCESS(code = 1)错误的主从应用程序

问题描述:

当我更改详细视图时,我的iPad应用程序崩溃。我设置了一个异常断点来查明导致问题的代码行。它似乎发生得相当零星,所以我不确定发生了什么事情。有什么建议么?在我的主视图控制器EXC_BAD_ACCESS(code = 1)错误的主从应用程序

self.splitViewController.viewControllers = details; 
我didSelectRowAtIndexPath方法方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) 
    { 
     KFBDetailViewController *detailViewController = [[KFBDetailViewController alloc] initWithNibName:@"KFBDetailViewController_iPad" bundle:nil]; 
     UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController]; 

     NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy]; 

     [details replaceObjectAtIndex:1 withObject:detailNavigationController]; 

     self.splitViewController.viewControllers = details; 

     KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate]; 
     appDelegate.window.rootViewController = self.splitViewController; 
    } 

这也是另一种观点认为在这里做的:

例如,坠毁在这一行

appDelegate.splitViewController.viewControllers = details; 

以下是该选项的didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy]; 

    UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:webViewController]; 

    [details replaceObjectAtIndex:1 withObject:detailNav]; 

    KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate]; 

    appDelegate.splitViewController.viewControllers = details; 
    appDelegate.window.rootViewController = self.splitViewController; 
    appDelegate.splitViewController.delegate = webViewController; 

    [appDelegate.splitViewController viewWillAppear:YES]; 

    // Grab the selected item 
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]]; 

    NSLog(@"Channel Items: %@", [[channel items]objectAtIndex:[indexPath row]]); 

    // Construct a URL with the link string of the item 
    NSURL *url = [NSURL URLWithString:[entry link]]; 

    NSLog(@"Link: %@", [entry link]); 

    // Construct a request object with that URL 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 

    NSLog(@"URL: %@", url); 

    // Load the request into the web view 
    [[webViewController webView]loadRequest:req]; 
    webViewController.hackyURL = url; 
    NSLog(@"Request: %@", req); 

    // Set the title of the web view controller's navigation item 
    [[webViewController navigationItem]setTitle:[entry title]]; 

    NSLog(@"Title: %@", [entry title]); 
} 

编辑:应用程序似乎当我在主视图控制器表视图选择第一行,以显示初始细节视图然后选择另一行显示不同的内容崩溃。如果我不在任何时候选择第一行,一切正常。

+0

为什么要将窗口的根视图控制器设置为分割视图控制器?它不是已经是根视图控制器吗? – rdelmar

+0

您可能会发现这有助于调试EXC_BAD_ACCESS错误,因为我有时会这样做。它的要点是在调试器中使用'register read'命令并查看寄存器以确定崩溃之前调用的最后一个函数。从中你可以确定你正在尝试使用该功能的对象。而且一旦你知道哪个交易对象正在被访问,你可以想出如何安全地调用它或者以某种方式保留它。 http://sealiesoftware.com/blog/archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html – jmathew

+0

你可能还需要这个http://lldb.llvm.org/lldb-gdb.html我发布的链接。 – jmathew

我设法通过调整在主视图中点击第一行时如何显示初始详细视图来解决问题。

您可以在我的原始文章中看到原始实施。这是我如何改变它。

if (indexPath.row == 0) 
    { 
     KFBDetailViewController *detailViewController = [[KFBDetailViewController alloc]initWithNibName:@"KFBDetailViewController_iPad" bundle:nil]; 

     NSMutableArray *details = [self.splitViewController.viewControllers mutableCopy]; 

     UINavigationController *detailNav = [[UINavigationController alloc]initWithRootViewController:detailViewController]; 

     [details replaceObjectAtIndex:1 withObject:detailNav]; 

     KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate]; 
     appDelegate.splitViewController.viewControllers = details; 
     appDelegate.window.rootViewController = self.splitViewController; 
     appDelegate.splitViewController.delegate = detailViewController; 
     [appDelegate.splitViewController viewWillAppear:YES]; 
    }