0

Ok, I have been following along with a tutorial and I have completed that, all works fine. However the initial view that loads is a UITableViewController and I would like a UIViewController.

Here is the code for that:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    
    window?.rootViewController = UINavigationController(rootViewController: ViewController())
    
    return true
}

I have tried editing this line:

UINavigationController(rootViewController: ViewController())

to:

window?.rootViewController = UIViewController(rootViewController: ViewController())

But then I am given this error:

Incorrect argument label in call (have 'rootViewController:', expected 'coder:')

It then asks me to 'Fix-it' so I do, which changes the line to:

window?.rootViewController = UIViewController(coder: ViewController())

But then this now throws the error:

Cannot convert value of type 'ViewController' to expected argument type 'NSCoder'

I have also tried:

window?.rootViewController = ViewController()

but with that, the simulator goes black.

Clarify Question:

How do I get the first View that loads in my app to be of type UIViewController?

Community
  • 1
  • 1
JamesG
  • 1,552
  • 8
  • 39
  • 86

1 Answers1

1

You should subclass UIViewController and make your own version to check it works, but what you're doing originally is fine

let myViewController = SomeViewController()
let navigationController = UINavigationController(rootViewController: myViewController)
window?.rootViewController = navigationController

Then in SomeViewController viewDidLoad set view.backgroundColor = .red

If you want to remove the navigation bar you can set it to hidden

navigationController.navigationBarHidden = true

alternatively...

let myViewController = SomeViewController()
window?.rootViewController = myViewController

Will also work... though you should be looking to keep the navigation controller in general.. it usually makes presenting view controllers a lot easier in the future...

The reason your simulator goes black is because it worked... you're showing an empty UIViewController... You must make your own UIViewController subclass and add stuff to it.

Your View Controller subclass should look like this

//
//  SomeViewController.swift
//  SomeProject
//
//  Created by Magoo on 17/10/2016.
//  Copyright © 2016 Magoo. All rights reserved.
//

import UIKit

class SomeViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()
        view.backgroundColor = .red

        let label = UILabel(frame:view.bounds)
        label.textColor = UIColor.whiteColor()
        label.text = "Hello world"

        view.addSubview(label)
    }
}

The result should be a red screen with 'Hello world' written in the centre.

Magoo
  • 2,552
  • 1
  • 23
  • 43
  • Sorry, slightly new. What do you mean: 'You must make your own UIViewController subclass and add stuff to it.' could you explain? I have already created a new file of type UIViewController which has 'class SomeViewController: UIViewController {' – JamesG Oct 17 '16 at 15:02
  • In XCode, go to File -> New -> File and choose Swift File. Then Call it SomeViewController.swift ... that file will then be available to you in your project... and you can edit it however you'd like. – Magoo Oct 17 '16 at 15:04
  • Basically everything that you want to show on the screen in this case needs to be in `SomeViewController` that's the view that's displayed. – Magoo Oct 17 '16 at 15:08
  • Thank you for your awesome and detailed explanation. Much Appreciated. – JamesG Oct 17 '16 at 15:13
  • 1
    You're welcome. I added a label too to better explain my point – Magoo Oct 17 '16 at 15:13