skip to Main Content

Now I’m learning swift using Xcode, but I don’t know 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?

see attached image

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
        }
    }
    
    Login or Signup to reply.
  3. 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".

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search