How does UIButton addTarget self work?
Asked Answered
O

1

5

I try figure out why self point to the GameViewController instead of Answer

GameViewController.swift

class GameViewController: UIViewController {
    var gameplay = QuestionsController(colors: colors)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(gameplay.answersController.answers[0].button)
    }

    func didPressAnswerButton(sender: UIButton!) {
        sender.setTitle("Im from GameViewController class", forState: .Normal)
    }
}

QuestionsController.swift

class QuestionsController {
    var question: Question
    var answersController: AnswersController
}

AnswersController.swift

class AnswersController {
    var answers = [Answer]()

    func prepareAnswers() {
        let answer = Answer()
        answers.append(answer)
    }
}

Answer.swift

class Answer{
    let button: UIButton

    func prepareButton() {
        let answerButton = AnswerButton(type: .System)
        answerButton.addTarget(self, action: "didPressAnswerButton:", forControlEvents: .TouchUpInside)
        button = answerButton
    }

    func didPressAnswerButton(sender: UIButton!) {
        sender.setTitle("Im from Answer class", forState: .Normal)
    }
}
Ofori answered 25/2, 2016 at 19:55 Comment(0)
B
15

addTarget:action:forControlEvents: tells the control (answerButton in this case) what method to call, and what object to call it on, when the user taps the button. Looking at your code in more detail:

 answerButton.addTarget(self, action: "didPressAnswerButton:", forControlEvents: .TouchUpInside)
  • When the user taps a button, the TouchUpInside event fires on the answerButton, and when that happens we want to invoke a method didPressAnswerButton: on an Answer object
  • So, we need to tell answerButton what do do when this TouchUpEvent fires. You do this calling the addTarget:action:forControlEvents method on the answerButton
  • The self argument tells the answerButton what object to notify about the event: it is the target. In this context, self is an Answer object.
  • The "didPressAnswerButton:" argument indicates what method the answerButton should call in response to the tap event: this is the action

This is the target-action mechanism of Objective-C/Cocoa. It's a very common pattern, it's worth it to read the linked documentation to learn a bit more about how it works. The key is that this is based on Objective-C* message passing: in the code above, "didPressAnswerButton:" indicates a selector, which when paired with a target (self), tells the answerButton how to send a "message" to the target when the user taps the button.

Also, note that when you are editing a storyboard and ctrl-drag from a button to your view controller and select a method, you are also setting up a target/action using this same mechanism. You select the target object by dragging to the view controller icon (or some other icon), and then you pick the action/selector when clicking on a method name in the popup.

* Target-Action was originally designed for Objective-C, but for the common case of implementing a view controller, you can assume Swift works the same way. Just note when reading documentation that Swift uses simple strings for actions, whereas Objective-C uses @selector(...).

Brinson answered 25/2, 2016 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.