Nazish Ali
Nazish Ali

Reputation: 422

Not able to show first controller iOS Swift

Below is my code, I am unable to load first view controller through this suggest me without using navigation controller embedded.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

    let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let firstViewController = storyBoard.instantiateViewControllerWithIdentifier("LoginViewController")
    self.window?.rootViewController = firstViewController
    return true
}

Upvotes: 0

Views: 74

Answers (4)

iOS Geek
iOS Geek

Reputation: 4855

1) make sure you are filling in the correct name for your storyboard. 2) check the identifier name for your view controller.

then,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as UIViewController

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()

    return true
}

Upvotes: 1

MudOnTire
MudOnTire

Reputation: 506

if you use storyboard, you gotta set the initial view controller in storyboard, "self.window?.rootViewController " won't work enter image description here

Upvotes: 0

Joe Benton
Joe Benton

Reputation: 3753

Do you need to do it via code? As your view controller is in storyboard you can just click on the view controller and tick the box to make it the initial view controller, which achieves the same thing as your code:

enter image description here

Upvotes: 1

Mr. Bond
Mr. Bond

Reputation: 427

let storyBoard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

change here your storyboard name.

Upvotes: 0

Related Questions