开发者

EXC_BAD_ACCESS Trying to Load UITableView Inside UIView Controller

开发者 https://www.devze.com 2023-03-25 23:17 出处:网络
This is starting to drive me insane, as I\'ve followed every instruction I can find online and still can\'t get this to work- all I\'m try开发者_如何学编程ing to do is have a view controller with a vi

This is starting to drive me insane, as I've followed every instruction I can find online and still can't get this to work- all I'm try开发者_如何学编程ing to do is have a view controller with a view that contains a tableview, and have the view controller handle the tableview's operations (i.e. be the delegate and data source for it).

I've set up my view controller and its view, added a tableview to the view in IB, created and connected an IBOutlet from the view controller to the tableview, and set the tableview's data source and delegate to the view controller. I've also made sure the view controller implements the UITableViewDelegate and UITableViewDataSource protocols in the interface file.

The issue occurs when I then go to add my view controller's view to the window. I get an EXC_BAD_ACCESS error on the return UIApplicationMain... line:

#import <UIKit/UIKit.h>

#import "ClareStreamsAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([ClareStreamsAppDelegate class]));
    }
}

So basically I'm wondering what I'm doing wrong here. As far as I can see I've connected everything in Interface Builder, but is there some other initialization I need to do in the code? And also another interesting point- if I don't connect the data source and delegate outlets from the tableview in IB, the error does not occur and the view loads fine (although obviously I then can't do anything with the tableview).

Thanks for any help!


I think I know what is causing your issues. I had the same problem awhile ago. What was happening was, my UIViews were being deallocated by ARC. For example, try adding this to your UIView's .m file and see if it gets called:

- (void)dealloc
{
    NSLog(@"This bitchass ARC deleted my UIView.");
}

The resolution is quite simple. Instead of allocating your UIView as an instance variable, like this...

- (IBAction)buttonTouchedUpInside
{
    ViewWithTableView *viewWithTableView = [[ViewWithTableView alloc] init];
    [self.view addSubview:viewWithTableView.view];
}

...you should allocate your UIView in the parent UIView that loads your UIView:

- (IBAction)buttonTouchedUpInside
{
    self.viewWithTableView = [[ViewWithTableView alloc] init];
    [self.view addSubview:viewWithTableView.view];
}


EXC_BAD_ACCESS indicates that you've over-released some variable. First, look at the log output and check for messages. The most common cause of this is incorrectly managing your ivars. Make sure to always access ivars through your accessors except in init and dealloc.

The most common way that people mess this up is code like this, where foo is an ivar:

foo = [NSString stringWithFormat:...]; // Or any method that returns an autorelease

The correct code is:

self.foo = [NSString stringWithFormat:...];

EDIT I overlooked that you're using ARC. First, make sure you're running the very latest betas, and double-check your logs for the error. Make sure you're always using strong/weak for your properties rather than retain/assign. Read the "Programming with ARC release notes."


The data source for the tableview is being edited without reloading the tableview.

Best guess.


Try this:

@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, [NSStringFromClass([ClareStreamsAppDelegate class]) autorelease]);   
}

The call to NSStringFromClass returns you an autoreleased NSString, but not necessarily in your autorelease pool. If NSStringFromClass sets up its own pool, the string it is returning is getting released, so when the code in your UIApplicationMain sends a message to it you get an EXC_BAD_ACCESS.

0

精彩评论

暂无评论...
验证码 换一张
取 消