How to init a button in iOS?

To init a button in Objective-C I do the following (old fashion style) :


------------------------

@interface …

{

IBOutlet NSButton *__strong aButton;

}

@property(strong) NSButton *__strong aButton;

------------------------

@implementation …


@synthesize aButton;

------------------------


How can this be done in iOS and Swift?

Answered by Claude31 in 410008022

You can also do it by code. Here is an example


          var loginButton: UIButton
          loginButton = UIButton(frame: CGRect(x: 80, y: 30, width: 100, height: 30))
          loginButton.setTitle("Tweet", for: UIControl.State.normal) 
          loginButton.addTarget(self, action: #selector(buttonTweetAction), for: UIControl.Event.touchUpInside)
          view.addSubview(loginButton)     // Add the button to the view

Put one line in the right class:

    @IBOutlet var aButton: UIButton!
Accepted Answer

You can also do it by code. Here is an example


          var loginButton: UIButton
          loginButton = UIButton(frame: CGRect(x: 80, y: 30, width: 100, height: 30))
          loginButton.setTitle("Tweet", for: UIControl.State.normal) 
          loginButton.addTarget(self, action: #selector(buttonTweetAction), for: UIControl.Event.touchUpInside)
          view.addSubview(loginButton)     // Add the button to the view

thank you OOP for your help

Thank you Claude31. In particular for the additions you made. Manuals a very succinct and for occational programmers often no help. To much implicit information resp. abbreviations. More explanations or examples like the one you gave are needed.

gefa

Yes, unfortunately, documentation is often succint and assumes you know a lot. Not really good for beginners. That's why I used a lot ray Wandelich tutorials when I need to learn on a new area.


ANyway, I hope your problem is solved.


If so, don't forget to close the thread by marking the correct answer.

Hello Claude31,


thank you for your kind reply. One question remains but not important for the solution of my problem:

What do you mean by "… ray Wandelich tutorials …" ?


gefa

Claude31 is referring to raywenderlich.com.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

When I search for a tutorial, I just Google "Ray Wenderlich some topic" …

How to init a button in iOS?
 
 
Q