views:

48

answers:

1

Hi

I have two universal applications... one is giving me an EXC_BAD_ACCESS error when i do this in the app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.
ScrollViewController *vc = [[ScrollViewController alloc] init];
[window addSubview:vc.view]; // EXC_BAD_ACCESS here
[window makeKeyAndVisible];

return YES;

}

I do exactly the same (same code, same scroll view controller class) in my other application and get no errors... my scroll view loads fine.

This problem is driving me insane.

Thanks

EDIT:

Here is the ScrollViewController Implementation:

@implementation ScrollViewController

- (void)loadView {
[super loadView];
UIScrollView *s = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];

NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"JCEKKeyboard" owner:self options:nil];

UIView *v = [a objectAtIndex:0];

[s setContentSize:CGSizeMake(400, 500)];
[s addSubview:v];
[[self view] addSubview:s];
}
+1  A: 

init shouldn't be creating the view, loadView does that. Calling the view getter (vc.view) when view is nil will cause loadView to be invoked.

Read the documentation for loadView, you are using it incorrectly. You should not call super from loadView and you must set the view property in the view controller. You should not invoke the getter for view [self view] from inside loadView, because the getter calls loadView. You are supposed to create the view in loadView and set the view property.

Something like this:

- (void)loadView {
    NSArray *a = [[NSBundle mainBundle] loadNibNamed:@"JCEKKeyboard" owner:self options:nil];  
    UIView *v = [a objectAtIndex:0]; 

    CGRect frame = [UIScreen mainscreen].applicationFrame;
    UIScrollView *s = [[UIScrollView alloc] initWithFrame: frame];
    [s setContentSize:CGSizeMake(400, 500)];
    [s addSubview:v];

    self.view = s;
}
progrmr
I changed it to self.view=s as you suggested and got it working, but if i dont call super then the app crashes on launch.
joec
Do you have a nib for that view built in IB? If so, you are not supposed to use loadView. Let the system load the nib and you can add anything you need in viewDidLoad, not in loadView. Read the Discussion part of the doc for loadView, I provided a link to it in my answer.
progrmr
I'm loading the scroll view programmatically using loadView, then adding a sub view to it using loadNibNamed
joec