11

I connected a IBOutlet and IBAction to my button variable from Interface Builder to my View Controller. How do I add an action method to the button in Swift? This code doesn't seem to work.

@IBOutlet var OK: UIButton!
@IBAction func OK(sender: UIButton){}

The Objective-C equivalent I found is:

@interface Controller
{
    IBOutlet id textField; // links to TextField UI object
}

- (IBAction)doAction:(id)sender; // e.g. called when button pushed

4 Answers 4

13

When you attach a button to the viewController and create an action (IBAction) using ctrl-drag, you create a method that looks likes this in Swift (if it doesn't have arguments):

@IBAction func buttonAction() {}

In Objective-C the same thing will look like this:

- (IBAction)buttonAction {}

So that means that @IBAction func OK(sender: UIButton){} is an action method.

If you want to know about the sender argument, I would recommend this SO post.

Edit:

For what you want to do, I create an IBOutlet and an IBAction, that way I can change its attributes with the outlet variable, and have the action side of things with the IBAction, like what you show above:

@IBOutlet var OK: UIButton!
@IBAction func OK(sender: UIButton){} 

For example, if I want to hide the button, I would put this code in the viewDidLoad

OK.hidden = true

The OK in that code is for the outlet variable, if I wanted to print "You pressed me" to the console when the button is pressed, I would use this code:

@IBAction func OK(sender: UIButton){
  println("You pressed me")
}

Above I am using the action to print "You pressed me" to the console.

A few things to note:

When Swift 2.0 gets released println will get changed to print. Also with you action and outlet, I would suggest giving them differing names, to make it easier to differentiate the two, something like this:

@IBOutlet var okOutlet: UIButton!
@IBAction func okAction(sender: UIButton){} 

Along with that, you should use camel case when naming variables, constants, functions, etc.

1
  • 1
    The solutions mentioned above works if I create a new button of Action type. But I created a button of type outlet and wanted to add an action to it. The reason I created outlet button is that I wanted a variable for the button so that I can set properties for the button variable. Now I also want to add action to it.
    – linux-user
    Sep 15, 2015 at 21:26
9

One way to do it, is control-drag from your button to your viewcontroller and choose action: enter image description here

If you have connected your button's action, your code should work just fine.

1
  • 1
    Connect the same button twice? One for Outlet and one for Action?
    – linux-user
    Sep 16, 2015 at 1:45
5

Here are the steps you can follow-

For @IBOutlet

1.Declare Your Interface builder Element property right after class name

class SomeViewController: UIViewController{

    @IBOutlet weak var aTextField : UITextField! ////Your Interface builder Element

2.Hook the IB Element From Storyboard.

enter image description here

For @IBAction

1.Write A method inside your class(say SomeViewController)

@IBAction func anAction(_sender : AnyObject){


}

2.Hook the method from Storyboard.

enter image description here

Hope it might helps.

3

You can simply add action from your storyboard. See the image.Image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.