Why has my designated UIViewController initializer never called?

I created a new Xcode project with a view-based application template. In the .m view controller, I rewrote this:

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization

        NSLog(@"initWithNibName...");
    }
    return self;
}


- (id)init {
    NSLog(@"initWithNibName...");
    return [super init];
}

However, I never get this NSLog. This is also my experience from other projects. This method is never called. Not even from NSObject.

The view controller is created inside the XIB file. Is it possible that the Nib Loading System instantiates this class without any initialization?

Why? What is the alternative to -loadView and -viewDidLoad?

Another great reason to stay away from XIB files?

+3
source share
3 answers

iOS

iPhone OS , NSCoding, initWithCoder:. UIView UIViewController, Interface Builder .

: , Interface Builder. , nib, . , nib, nib.

, , . (-initWithCoder:) , nib, , , . NSCoding, .

+9

- awakeFromNib. Init , .

+1

MainWindow appDelegate..? UIApplicationDelegate AppDelegate applicationDidFinishLaunching Methode, initWithNibName. .

0

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


All Articles