Eamorr
Eamorr

Reputation: 10022

xib refuses to show

So I'm trying to execute the following Swift code:

class Login: UIViewController{
...

    func loginIsCorrect(){
        /*problem code:*/
        let main = Main(nibName: "Main", bundle: nil)
        self.navigationController?.pushViewController(main, animated: true)   //will not show!
        /*end problem code*/
        println("Hello")
    }
 }

I can see "Hello" printed in the console, but the Main xib just will not appear (the iPhone remains on the Login.xib)

Here's my AppDelegate.swift:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var rootViewController :UIViewController?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        rootViewController  = Login(nibName:"Login",bundle:nil)
        window!.rootViewController = rootViewController
        window!.makeKeyAndVisible()

        return true
    }
    ...
}

And here's my vanilla Main.swift (nothing implemented yet):

import Foundation
import UIKit


class Main: UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Upvotes: 0

Views: 47

Answers (2)

topher91
topher91

Reputation: 222

You haven't setup a navigation controller so navigation controller is nil. You have used a ? in the call so the it doesn't blow up. If you change it to a ! your app will crash.

self.navigationController!.pushViewController(main, animated: true)

In you appdelegate add the Login view controller to a UINavigationController and make that your rootViewController

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let loginVC = Login(nibName:"Login",bundle:nil)
    rootViewController  = UINavigationController(rootViewController: loginVC)
    window!.rootViewController = rootViewController
    window!.makeKeyAndVisible()

    return true
}

Upvotes: 0

Stee
Stee

Reputation: 29

It looks a lot like you are trying to push your Main view controller onto a non-existent navigation controller.

Also please rename Login and Main LoginViewController and MainViewController for the sake of readability.

Upvotes: 1

Related Questions