user470763
user470763

Reputation:

Initialising a view controller in Swift

So my code works but I get a warning:

Null passed to a callee that requires a non-null argument

Obviously this is because of the following line:

MyViewController *vc = [[MyViewController alloc] initWithCoder:nil];

MyViewController is a Swift class. I have implemented the required initWithCoder method in it.

My question is simple - what parameter do I pass to initWithCoder to kill the warning or what alternate init strategy do I use?

NB: The view controller I'm initialising is written in Swift. I am however initialising it from an Objective-c class.

Upvotes: 1

Views: 1641

Answers (2)

Tommie C.
Tommie C.

Reputation: 13181

Given that your view controller has no nib or storyboard you should not be using initWithCoder in Swift. Note this information from the UIViewController documentation:

Discussion

This is the designated initializer for this class.

The nib file you specify is not loaded right away. It is loaded the first time the view controller's view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there.

If you specify nil for the nibName parameter and you do not override the loadView method, the view controller searches for a nib file using other means. See nibName.

If your app uses a storyboard to define a view controller and its associated views, your app never initializes objects of that class directly. Instead, view controllers are either instantiated by the storyboard either automatically by iOS when a segue is triggered or programmatically when your app calls the storyboard object’s instantiateViewControllerWithIdentifier: method. When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its initWithCoder: method instead. iOS automatically sets the nibName property to a nib file stored inside the storyboard.

For more information about how a view controller loads its view, see Resource Management in View Controllers.

Here is what Resource Management says about initializing programmatically.

Initializing View Controllers Programmatically If a view controller allocates its resources programmatically, create a custom initialization method that is specific to your view controller. This method should call the super class’s init method and then perform any class specific initialization.

In general, do not write complex initialization methods. Instead, implement a simple initialization method and then provide properties for clients of your view controller to configure its behaviors.

Upvotes: 1

Azat
Azat

Reputation: 6795

You should do it in a different way. If you use storyboards:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];

If storyboard is different

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];

Or from xib:

MyViewController *controller = [[MyViewController alloc] initWithNibName:nibName bundle:nil];

Or if it is from code totally:

MyViewController *controller = [[MyViewController alloc] init];

Also remove -initWithCoder: method from your code

Upvotes: 1

Related Questions