Home > other >  Where should I drag and drop "label"?
Where should I drag and drop "label"?

Time:07-21

enter image description here

Now I'm learning swift using xcode, but idk where to drag and drop and why drag and drop "label" to somewhere

should I drop no.1? or no.2? or no.3? and why I should drop there?

CodePudding user response:

If you drop it to 1, Xcode automatically generate an @IBOutlet for you. If you drop it to 2 or 3, then @IBAction. There's no other reason for that except that Xcode tries to be smart and help you organize code more nicely: properties with properties in the top of the class, methods – in the methods area. And you can also move declarations to another place later: except for code style matters, it doesn't matter, where exactly within your class you put declarations.

CodePudding user response:

as you are trying to take an IBOutlet for a label , its nice to keep it on top of the class (where you mention 1).

normally Xcode gives suggestion you , if you drug on top side it will be @IBOutlet or you on bottom like 2 or 3 it will be @IBAction like you already took a button action in your code .

Your Solution On Code:

import UIKit
class CodePresentViewController: UIViewController {
    
    @IBOutlet weak var YourLabel: UILabel! // insert @IBOutlet type property here 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    @IBAction func tapBackButton(_ sender: UIButton) {
        //Yout Button COde
    }
}

CodePudding user response:

If you want to own the label in your code, and then make some configuration to the label by code, use "1". It will give you a @IBOutlet label object in your code. If you want to set the label's action, use "2" or "3".

  • Related