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)
}
}
addTarget:action:forControlEvents:tells the control (answerButtonin 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:TouchUpInsideevent fires on theanswerButton, and when that happens we want to invoke a methoddidPressAnswerButton:on anAnswerobjectanswerButtonwhat do do when this TouchUpEvent fires. You do this calling theaddTarget:action:forControlEventsmethod on theanswerButtonselfargument tells theanswerButtonwhat object to notify about the event: it is the target. In this context,selfis anAnswerobject."didPressAnswerButton:"argument indicates what method the answerButton should call in response to the tap event: this is the actionThis 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(...).