raginggoat
raginggoat

Reputation: 3600

EXC_BAD_ACCESS(code=1) Error in Master-Detail App

My iPad app is crashing when I change the detail view. I set an exception breakpoint to pinpoint the line(s) of code causing the issue. It seems to happen rather sporadically so I'm not really sure what's going on. Any suggestions?

For example, it crashed on this line:

self.splitViewController.viewControllers = details;

in my didSelectRowAtIndexPath method in my master view controller

- (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;
    }

It also did it here in another view:

appDelegate.splitViewController.viewControllers = details;

Here is the didSelectRowAtIndexPath for that one:

- (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]);
}

EDIT: The app seems to crash when I select the first row in the master view controller table view to show the initial detail view then select another row to show different content. Everything works fine if I don't select the first row at any point.

Upvotes: 1

Views: 417

Answers (1)

raginggoat
raginggoat

Reputation: 3600

I managed to fix the problem by adjusting how I show the initial detail view when tapping the first row in master view.

You can see the original implementation in my original post. Here is how I changed it.

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];
    }

Upvotes: 1

Related Questions