Chase Roberts
Chase Roberts

Reputation: 9376

Swift create a button as a class variable / member variable

This should be super straightforward. Here's what I am trying to do:

class ViewController: UIViewController{
    var leftBarButton: UIButton;
    var rightBarButton: UIButton;

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

But I am getting an error that viewController has no initializers. I know I need to give the variables default values, so I tried:

var rightBarButton: UIButton(type: UIButtonType.System) as UIButton;

But that's giving me an error about multiple declarations.

Upvotes: 0

Views: 83

Answers (1)

Stefan Salatic
Stefan Salatic

Reputation: 4513

You need something like this

var rightBarButton: UIButton = UIButton(type: UIButtonType.System)

You need to intialize the button, but if you're not planning to change it I suggest you change it to let

Upvotes: 1

Related Questions