Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storyboard! vs. UIStoryboard(name: "Main", bundle: nil)

Tags:

ios

swift

I got put into a project that has the following declaration:

UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("asdf") as! AsdfViewController

I've always used storyboard! to do this. It is my understanding that storyboard! will always give me UIStoryboard(name: "Main", bundle: nil). Is this a dangerous assumption to make?

I know that you may want to use the official constructor for UIStoryboard if you wanted to reference a different storyboard file, but is it safe to abbreviate it to storyboard! if you are sure that the VC you wish to instantiate is part of Main.storyboard?

like image 480
Kelvin Lau Avatar asked Sep 29 '15 18:09

Kelvin Lau


1 Answers

If you are calling self.storyboard from a view controller that is on another storyboard file, it will give reference to that particular storyboard; not the main storyboard. By mentioning:

UIStoryboard(name: "Main", bundle: nil)

You can make sure that you are referencing the main storyboard always.

In following situations:

  1. Have only one storyboard file
  2. Instantiate a view controller which is also included in the current view controller's storyboard

using self.storyboard is enough.

like image 145
Midhun MP Avatar answered Oct 09 '22 14:10

Midhun MP