Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewController init?

I have just noticed that my ViewController does not call init (See below) when it starts up.

-(id)init {
    self = [super init];
    if(self) {
        NSLog(@"_init: %@", [self class]);
        otherStuff...
    }
    return self;
}

Is there a reason for this, or is it replaced by viewDidLoad

-(void)viewDidLoad {
    otherStuff ..
    [super viewDidLoad];
}

cheers gary

like image 745
fuzzygoat Avatar asked Mar 26 '10 17:03

fuzzygoat


People also ask

What is INIT in SwiftUI?

By default, SwiftUI assumes that you don't want to localize stored strings, but if you do, you can first create a localized string key from the value, and initialize the text view with that. Using a key as input triggers the init(_:tableName:bundle:comment:) method instead.

What is ViewController in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.

What is init coder?

init(coder:)Returns an object initialized from data in a given unarchiver.

What is required init in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.


2 Answers

It's not replaced by viewDidLoad. It's just that init's not called by initWithNibName:bundle:.

I just write my setup code in viewDidLoad.

like image 172
Frank Shearar Avatar answered Sep 30 '22 23:09

Frank Shearar


If your view controller is being initialized from a storyboard, then you can use:

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // do init here
    }
    return self;
}
like image 28
ThomasW Avatar answered Sep 30 '22 22:09

ThomasW