5

I have a simple example. I connected the left button1 and label1 with ctrl-draging it to the contollerclass.

How can I do the same for the right button2 label2 programaticly (without ctrl-draging)

Xcode

That´s my code:

class ViewController: NSViewController {

  @IBOutlet weak var label1: NSTextField!  //connected with ctrl-drag
  @IBOutlet weak var button1: NSButton!    //connected with ctrl-drag

  @IBOutlet weak var label2: NSTextField!  //not yet connected
  @IBOutlet weak var button2: NSButton!    //not yet connected

  @IBAction func button1Pressed(_ sender: Any)  //connected with ctrl-drag
  { label1.stringValue = "button-I"
    button1.title = "pressed"
  }

  @IBAction func button2Pressed(_ sender: Any)  //not yet connected
  { label2.stringValue = "button-II"
    button2.title = "pressed"
  }
  override func viewDidLoad() {
    super.viewDidLoad()
  }
}
mica
  • 3,898
  • 4
  • 34
  • 62
  • 1
    You don't need `IBAction` and `IBOutlet` when you create it in code. And you don't do "ctrl-drag" in code. You write actual code to create the control and assign the action method. – rmaddy May 24 '17 at 15:20
  • I want to use storyboard to lay out all elements there, only the connections should by created programatically, but that seems not to work ;-( – mica May 24 '17 at 15:54
  • You could write code that access the storyboard and then accesses the control and assigns it to the outlet variable. But why would you do all of that when you can simply connect the outlet in the storyboard? – rmaddy May 24 '17 at 15:56
  • The intention of the idea was, to have more "code/design" in an more explicit form as Swift-code that can e.g. be diffed or fast transported in text form, without loosing the comfort of designing in storyboard. I made the experience, that it could be quite hard to find a bug in IB settings. Maybe a bad idea ;-) – mica May 24 '17 at 16:03

1 Answers1

2

If you want to use storyboard and lay out all your elements there, you can't really avoid the ctrl drag (or just drag if you open the connections inspector on the right).

You could however create your second button programmatically in code and not use the storyboard at all for it. Then programmatically add your constraints too.

You can also add the action of the button programmatically (using addTarget) if you would like but that would require at least the IBOutlet to be setup in order to have a reference to the button.

Prientus
  • 733
  • 6
  • 15
  • 1
    If you have the IBOutlet reference to the button can you not still use addTarget() to point to your desired function? Seems a little pointless I must say seeing as if you are going to setup the IBOutlet you might as well setup the IBAction as well. – adamfowlerphoto May 24 '17 at 15:29
  • 1
    Oh absolutely. The question though I believe is avoiding both IBOutlet and IBAction. I will update my answer to include your point though as it is a valid one. – Prientus May 24 '17 at 15:32
  • Indeed I would like to use storyboard and lay out all elements there. Bad there is no way to make the connection programatically. – mica May 24 '17 at 15:50