简体   繁体   English

UIButton addTarget如何自行工作?

[英]How does UIButton addTarget self work?

I try figure out why self point to the GameViewController instead of Answer 我试着搞清楚为什么self指向GameViewController而不是Answer

GameViewController.swift 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 QuestionsController.swift

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

AnswersController.swift AnswersController.swift

class AnswersController {
    var answers = [Answer]()

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

Answer.swift 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 ( answerButton in this case) what method to call, and what object to call it on, when the user taps the button. addTarget:action:forControlEvents:告诉控件(在这种情况下为answerButton )当用户点击按钮时,调用什么方法以及调用它的对象 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 当用户点击一个按钮时, TouchUpInside事件将在TouchUpInsideanswerButton ,当发生这种情况时,我们想要在Answer对象上调用方法didPressAnswerButton:
  • So, we need to tell answerButton what do do when this TouchUpEvent fires. 因此,我们需要告诉answerButton当TouchUpEvent触发时该怎么做。 You do this calling the addTarget:action:forControlEvents method on the answerButton 你这样做就是在answerButtonanswerButton addTarget:action:forControlEvents answerButton方法
  • The self argument tells the answerButton what object to notify about the event: it is the target . self参数告诉answerButton要通知的关于事件的对象:它是目标 In this context, self is an Answer object. 在这种情况下, self是一个Answer对象。
  • The "didPressAnswerButton:" argument indicates what method the answerButton should call in response to the tap event: this is the action "didPressAnswerButton:"参数指示answerButton响应tap事件应调用的方法:这是动作

This is the target-action mechanism of Objective-C/Cocoa. 这是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. 关键是这是基于Objective-C *消息传递:在上面的代码中, "didPressAnswerButton:"表示一个选择器 ,当与目标( self )配对时,告诉answerButton如何向“...”发送“消息”用户点击按钮时的目标。

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. 另请注意,在编辑故事板并按住Ctrl键并从按钮拖动到视图控制器并选择方法时,您还可以使用相同的机制设置目标/操作。 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. * Target-Action最初是为Objective-C设计的,但是对于实现视图控制器的常见情况,您可以假设Swift以相同的方式工作。 Just note when reading documentation that Swift uses simple strings for actions, whereas Objective-C uses @selector(...) . 请注意,在阅读文档时,Swift使用简单的字符串进行操作,而Objective-C使用@selector(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM