IBOutlet and IBAction in Swift

I connected a IBOutletand IBActionto my button variable from Interface Builder to my view manager. How to add an action method to a button in Swift? This code does not work.

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

I found the equivalent of Objective-C:

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

- (IBAction)doAction:(id)sender; // e.g. called when button pushed
+4
source share
4 answers

One way to do this is to drag the control from your button to your viewcontroller and select an action: enter image description here

If you connected the action of your button, your code should work fine.

+7
source

viewController (IBAction) ctrl-drag, ( ), Swift ( ):

@IBAction func buttonAction() {}

objective-C :

- (IBAction)buttonAction {}

, , @IBAction func OK(sender: UIButton){} - .

, SO.

:

, , IBOutlet IBAction, , IBAction, , , :

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

, , viewDidLoad

OK.hidden = true

OK , " " , :

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

" " .

:

Swift 2.0 , println print. , , , , - :

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

, , , ..

+7

, -

For @IBOutlet

1.Declare Property of the interface. Element property right after the class name

class SomeViewController: UIViewController{

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

2. Lift the IB element from the storyboard.

enter image description here

For @IBAction

1. Write Method inside your class (say SomeViewController)

@IBAction func anAction(_sender : AnyObject){


}

2. Grab the method from the storyboard.

enter image description here

Hope this helps.

+3
source

You can simply add an action from your storyboard. See Image.Picture

+2
source

Source: https://habr.com/ru/post/1607333/


All Articles