SanjayPathak
SanjayPathak

Reputation: 121

Difference between instantiateViewControllerWithIdentifier and initWithNibName?

What is the difference between instantiateViewControllerWithIdentifier and initWithNibName, other than former is from storyboard and later is not?

UIStoryboard *signupStory = [UIStoryboard storyboardWithName:@"SignupStory" bundle:[NSBundle mainBundle]];
    SignupLoginViewController *signUpVC = [signupStory instantiateViewControllerWithIdentifier:@"SignupVC"];  

and

SignupLoginViewController *signUpVC = [[SignupLoginViewController alloc] initWithNibName:@"SignupLoginViewController" bundle:[NSBundle mainBundle]];

Upvotes: 1

Views: 588

Answers (1)

Dreaming In Binary
Dreaming In Binary

Reputation: 1287

If you've created your UI using storyboards, you'll want to call instantiateViewControllerWithIdentifier. Here, the identifier is not part of the view controller itself but is only used by the storyboard to locate the view controller. The storyboard will handle initialization and eventually call initWithCoder, which is why you need to override this when creating subclassed view controllers.

On the other hand, if your UI resides in a pure .xib file - developers generally use initWithNibName. It does, technically, break encapsulation and there are other ways to do it, but you'll see this used most often as it's the designated initializer for the class.

Upvotes: 7

Related Questions