user3723418
user3723418

Reputation:

View Controller not loading Storyboard View

I am new to iOS development. I have a story board with 2 view controllers, I link them up to UIViewController classes, it works when I try load the second class from the first class (like a splash screen going into the main menu) but then the 'menu' does not load the view from the storyboard. It loads a black screen. I have assigned the class to the viewcontroller in the right hand sidebar of the storyboard and coloured the screen red to see if it loads the class, it does, but if I take the red out, it loads a black screen, not the desired screen from the storyboard.

Screen 1's (Splash)'s code:

func switchScreen() {

    let secondViewController:vcMainLogin = vcMainLogin()
    self.presentViewController(secondViewController, animated: true, completion: nil)

}

override func viewDidLoad() {
    super.viewDidLoad()
    NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "switchScreen", userInfo: nil, repeats: false)    

}

Screen 2's (login / menu)'s code:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.redColor()
    // Do any additional setup after loading the view.
}

It is loading because the colour of the screen turns red, but when I take that out it loads black, not the screen from the storyboard.

Upvotes: 5

Views: 9331

Answers (2)

Brandon
Brandon

Reputation: 2427

You need to instantiate the view controller from the storyboard and not create it via the initialize function. To do this you need to assign your view controller a storyboardID in interface builder (view -> utilities -> identity inspector). In this example I load a view controller with a storyboard ID of "VC2" from the storyboard named "MainStoryboard".

let mainStoryboard = UIStoryboard(name: "MainStoryboard", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("VC2") as UIViewController

Here is the equivalent objective-c code for reference

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"mainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"VC2"];

Upvotes: 14

Hermann Klecker
Hermann Klecker

Reputation: 14068

I am not yet used to Swift. However, it looks ot me as if you load just instanticate a brand new mcMainLogin view controller instead of initializing it from a nib file or loading it from a story board. Naturally it would be empty (black).

Go back to the references or wait for somebody with Swift experience to tell you how to load the view controller from the storyboard.

If you create all your view controllers programmatically and do the navigation programatically then there is no need for storyboards at all. You'd be far better of either complying to the storyboard concept including navigation at least in 90+ % of all navigation events or work with individutal nib/xib files.

Upvotes: 0

Related Questions