bmilleker
bmilleker

Reputation: 83

Subclassing UIViewController, viewDidLoad called repeatedly

I subclassed UIViewController as STViewController and noticed that classes inheriting from STViewController have their viewDidLoad method being called repeatedly. Ultimately crashing the app. STViewController is basically a blank implementation at this point. I am subclassing as shown below:

#import "STViewController.h"

@interface WelcomeViewController : STViewController {

STViewController.h

#import <UIKit/UIKit.h>

@interface STViewController : UIViewController
{
}
@end

STViewController.m

#import "STViewController.h"


@implementation STViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

viewDidLoad() from WelcomeViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    // hide the buttons
    [[self signUp] setHidden: YES];
    [[self logIn] setHidden: YES];
}

Upvotes: 2

Views: 4998

Answers (1)

magma
magma

Reputation: 8520

You are overriding loadView, but your implementation is empty, and you're not assigning a view. Remove the loadView override.

From UIViewController Class Reference (emphasis mine):

You should never call this method directly. The view controller calls this method when the view property is requested but is currently nil. If you create your views manually, you must override this method and use it to create your views. If you use Interface Builder to create your views and initialize the view controller—that is, you initialize the view using the initWithNibName:bundle: method, set the nibName and nibBundle properties directly, or create both your views and view controller in Interface Builder—then you must not override this method.

The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view.

If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property. (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super.

Upvotes: 8

Related Questions