0

I am using the storyboard references in my project. When I tried to load view controller using self.storyboard it crashes. But When I created a new instance of storyboard and tried to load it works perfectly.

Another thing is without storyboard reference it works perfectly for the first case also.

I am using Xcode 7.3.1.

With self.storyboard. It crashes

guard let registerViewController = self.storyboard?.instantiateViewControllerWithIdentifier(Sto‌ryboardIdentifier.Re‌gistration) as? RegisterVC else { return }

This is the instance of UIStoryboard. It works.

let mainStoryBoard = UIStoryboard.init(name: "Main", bundle: nil)

    guard let registerViewController = mainStoryBoard.instantiateViewControllerWithIdentifier(StoryboardIdentifier.Registration) as? RegisterVC else {
        return
    }
Imad Ali
  • 3,261
  • 1
  • 25
  • 33
John
  • 238
  • 2
  • 11
  • 2
    You should add your code which is causing Crash. Also the crash report. – Sushil Sharma Oct 09 '17 at 07:20
  • show the code, at where the crash is happening ? – R. Mohan Oct 09 '17 at 07:20
  • guard let registerViewController = self.storyboard?.instantiateViewControllerWithIdentifier(StoryboardIdentifier.Registration) as? RegisterVC else { return } – John Oct 09 '17 at 07:26
  • Most probably, your current VC and VC that you'r loading are not in same VC. – dahiya_boy Oct 09 '17 at 07:27
  • @dahiya_boy it is same it works for second case – John Oct 09 '17 at 07:28
  • let mainStoryBoard = UIStoryboard.init(name: "Main", bundle: nil) guard let registerViewController = mainStoryBoard.instantiateViewControllerWithIdentifier(StoryboardIdentifier.Registration) as? RegisterVC else { return } It works without any problem. – John Oct 09 '17 at 07:29
  • 1
    Add code in your question not in comments, [Edit your Question](https://stackoverflow.com/posts/46640701/edit) – Imad Ali Oct 09 '17 at 07:30
  • 1
    @John He is saying "he is creating the instance of storyboard", and using self are both different thing. If you VC's are at diff storyboard then you have to create instance else if they are in same then you dont need to create instance, `self` will work. – dahiya_boy Oct 09 '17 at 07:31
  • @dahiya_boy Hi, You meant say if we use storyboard reference, we should create instance of storyboard. – John Oct 09 '17 at 07:37
  • Are you working on Single storyboard or you have multiple storyboards in your app ? Check, in which Storyboard did you add `RegisterVC` ? Also, add crash message in your question to get your question understandable. – Sushil Sharma Oct 09 '17 at 08:11
  • self.storyboard it will work only when scope of Storyboard in same . if multiple Storyboard present and your Storyboard scope is in out of current ViewController, it will get crash – Joyal Clifford Oct 09 '17 at 08:20
  • @John Pls check this [answer](https://stackoverflow.com/questions/46628492/why-instantiateviewcontoller-is-necessary/46628778#46628778), It helps you alot, If further you have doubt then ask. – dahiya_boy Oct 09 '17 at 08:24

1 Answers1

0

I'm going to go out on a limb here, since you are using Storyboard References, and say that mainStoryBoard and self.storyboard are referencing different StoryBoards, and the one that chokes does not contain the ViewController named StoryboardIdentifier.Registration.

Either that or StoryboardIdentifier.Registration contains a different value in each case.

CodeWriter23
  • 317
  • 3
  • 10