2

I have a UIViewController subclass called TripViewController. This class has the following method:

- (void)lockScreen {
  LockOverlay *lockOverlay = [[LockOverlay alloc] init];
  [self presentModalViewController: lockOverlay animated:YES];
}

LockOverlay is also a UIViewController subclass, defined as follows (the rest of the code is just auto-generated stubs):

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
  CGRect frame = CGRectMake(0, 0, 225, 37);
  UIImageView *sliderBackground = [[UIImageView alloc] initWithFrame:frame];      
  sliderBackground.image = [UIImage imageNamed:@"slider-bar.png"];  
  UIImageView *unlock = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unlock.png"]];
  [sliderBackground addSubview:unlock];
  frame = CGRectMake(10, 360, 225, 37);
  Slider *slider = [[Slider alloc] initWithFrame:frame];
  [slider addSubview:sliderBackground];
  slider.unlock = unlock;
  [self.view addSubview:slider];
}

When lockScreen gets called, the program goes into an infinite loop, and loadView gets called over and over.

So, what am I doing wrong here? I've had a bug like this before... In the App Delegate, I create a TabBarController, and one of the views has a NavigationController. I got the same sort of bug when I tried to add the View instead of the NavigationViewController to the tabBar array. I assume this problem is similar and I'm not pushing the new ViewController to the right place, but that's just a guess.

2 Answers 2

0

I changed loadView to be initWithFrame and returned self. It works now.

0

before

[self.view addSubview:slider];

you should have something like

self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

This is because self.view doesn't exist yet... that's what loadView is for! It's documented in the UIViewController docs that the implementation of the view property is that, when accessed (vs assigned) and so far it's nil, it calls loadView to create the view first. Your loadView was assuming a view exists and tried to add a subview, that access was causing the view controller to call loadView again.

An override of loadView should always assign self.view to some-view-that-fills-the-screen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.