Reputation: 1601
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.
How do I get the first View that loads in my app to be of type UIViewController?
Upvotes: 0
Views: 3041
Reputation: 2619
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.
Upvotes: 1