TheLazyChap
TheLazyChap

Reputation: 1902

Swift - Programmatically creating Views Issue, unable to find stored property

I am trying to learn Swift and iOS Views and ViewControllers.

var window: UIWindow?
var rootViewController: MyCustomView?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    // Override point for customization after application launch.

    self.rootViewController = MyCustomView()
    self.rootViewController!.backgroundColor = UIColor.orangeColor()

    var rect = CGRectMake(20, 20, 100, 100)
    var label = UILabel(frame: rect)
    label.text = "Hello iOS Views"
    label.backgroundColor = UIColor.orangeColor()

    self.window!.rootViewController = self.rootViewController
    self.window!.backgroundColor = UIColor.whiteColor()
    self.window!.makeKeyAndVisible()
    return true

I am getting an error when I compile, "Could not find member 'rootViewController'" on the following line:

self.window!.rootViewController = self.rootViewController

Not sure why Xcode 6 Beta is not liking it but it's able to find this line:

self.rootViewController!.backgroundColor = UIColor.orangeColor()

Upvotes: 2

Views: 869

Answers (1)

matt
matt

Reputation: 535925

The problem is that MyCustomView is a UIView. But UIWindow's rootViewController expects a UIViewController.

Generally you have confused yourself right through your code by not distinguishing view controllers from views. But you did name MyCustomView sensibly, which is good. The fact that it has a backgroundColor helps to prove that it is a view, not a view controller (view controllers have no background color).

Upvotes: 2

Related Questions