EXC_BAD_ACCESS in [addSubview window: viewcontroller.view]

I have two universal applications ... one gives me an EXC_BAD_ACCESS error when I do this in the application’s delta:

- (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 am doing the same thing (same code, same scroll controller class) in my other application and not getting any errors ... my scroll view loads fine.

This problem is driving me crazy. Here is the implementation of ScrollViewController:

@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];
}
+3
source share
2 answers

init should not create a view; loadView does this. Calling the getter function (vc.view) when view is zero will call loadView.

loadView, . super loadView, view . getter [self view] loadView, getter loadView. loadView view.

- :

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

, subviews, .. [someView addSubview:someOtherView], ( someOtherView). someView , . , - , . , .

gdb :

,

po [someView description]

someView

po [someView subviews]  

(, subview 0)

po [[someView subviews] objectAtIndex:0]

,

po [someView recursiveDescription]

, recursiveDescription, .

+2

Source: https://habr.com/ru/post/1754975/


All Articles