简体   繁体   中英

Swift 2: Instance Member cannot be used on type “View Controller”

Im new to swift and I'm experimenting with classes and methods! I have been looking all over google and stackoverflow to find an answer! I have read multiple posts with the same problem but they still don't help me! I have written swift code for a bigger app but decided to write a small portion so I get the same idea. Im trying to update a UILabel's text with a method inside a class when a certain button is pressed. Im trying to change the text by MyLabel.text = "text" but its giving me the error of 'Instance member cannot be used on type "view controller"' Please help me find whats wrong with it and explain it! Thank you so much! Here is my code bellow:

    class ViewController: UIViewController {

class Door {

    var DoorLocked = false

    func lockDoor() {
        DoorLocked = true
        MyLabel.text = "The door is locked!"
    }

    func unlockDoor() {
        DoorLocked = false
        MyLabel.text = "The door is unlocked!"
    }

    init() {
        MyLabel.text = "This is a door!"
    }
}

var DoorStatus = Door()

@IBOutlet weak var MyLabel: UILabel!

@IBAction func LockButton(sender: AnyObject) {
    DoorStatus.lockDoor()
}

@IBAction func UnlockButton(sender: AnyObject) {
    DoorStatus.unlockDoor()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

The inner class Door doesn't know anything about MyLabel . Inner classes don't share variables with the class they're declared in, unlike other languages. It's pretty much like the Door class is declared at top level along-side ViewController . You need a good bit more background to separate a model & ViewController into separate classes and then make them communicate properly with a protocol/delegate pattern. Unless you're following a template for exactly how, first just do everything within ViewController . So declare your model variable doorLocked directly within ViewController , and update it along with changing the text of the label directly within the @IBAction .

Again caveat that this is just learning iOS & Swift at a basic level, then proper MVC design should come next.

Also, all variables should start with lower-case. Every time you start a variable with uppercase, it hurts the eyes because it looks like a class or other type rather than storage.

From a architectural point of view, I would argue that it does not make sense for the Door class to know of any labels on that specific ViewController . Further, the Swift language, does not allow you to access that label inside the nested class. Instead, consider doing something like:

class ViewController: UIViewController {

    class Door {

        var DoorLocked = false

        func lockDoor() {
            DoorLocked = true
        }

        func unlockDoor() {
            DoorLocked = false
        }

    }

    var DoorStatus = Door()

    @IBOutlet weak var MyLabel: UILabel!

    @IBAction func LockButton(sender: AnyObject) {
        DoorStatus.lockDoor()
        MyLabel.text = "The door is locked!"
    }

    @IBAction func UnlockButton(sender: AnyObject) {
        DoorStatus.unlockDoor()
        MyLabel.text = "The door is unlocked!"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        MyLabel.text = "This is a door!"
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM