将按钮的目标和选择器设置为多种方法
我想添加多种方法作为按下按钮时的选择器响应。一个按钮可以有两个在按下按钮时调用的方法吗?
通过研究,我发现,在 Objective-C 编程语言指南中,按钮将调用与选择器同名的所有方法。
我希望我的按钮同时执行两个操作:
- 播放音频文件
在数组中显示视图。
UIBarButtonItem *play = [[UIBarButtonItem 分配] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay 目标:自己 action:@selector(play:)];
感谢建议。
谢谢
I want to add multiple methods in that respond as the selector when a button is pressed. Can one button have two methods that get called when the button is pressed?
Through my research, I found, in the objective-C Programming Language Guide, that a button will call All methods with the same name as the selector.
I want my button to do two actions at the same time:
- play the audio file
display views in array.
UIBarButtonItem *play = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(play:)];
Appreciate advice.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@selector()
字面上只是返回一个 SEL 值,它只是一个名称(事实上,在幕后,它实际上是一个字符串)。它没有指定任何特定行为。类选择在收到选择器时如何响应。您当然可以让一个类实现一个执行两件事的方法,并将该方法的选择器设置为控件的操作:
您还可以通过重复调用
addTarget:action:forControlEvents:
addTarget:action:forControlEvents::@selector()
literally just returns a SEL value, which is just a name (in fact, under the hood, it's literally a string). It doesn't specify any particular behavior. Classes choose how to respond when they're sent a selector.You could certainly have a class implement a method that does two things and set the selector for that method to be a control's action:
You can also add multiple actions to a control with repeated calls of
addTarget:action:forControlEvents:
:您可以为特定事件指定多个目标操作对。
You can specify multiple target-action pairs for a particular event.
每次向对象添加目标时,它都会创建一个控制对象(也称为“操作消息”)(控制对象是 UI 控件类型)。该控制对象包含称为“操作选择器”的选择器的名称以及需要调用该选择器的目标。然后,该控制对象与指定事件绑定(注册)。您可以将多个控制对象绑定到同一事件。这意味着我可以将 2 个目标和两个选择器绑定到同一事件。
示例
在运行时,绑定到给定事件的所有这些控制消息将被分派到适当的目标,换句话说,所有这些选择器方法将在其各自的目标类对象上调用。
Every time you add a target to a object, it creates a control object (also called 'action message') (control object is of type UI control). This control object contains the name of the selector called 'action selector' and the target on which this selector needs to be invoked. This control object then gets bind (registered) with a specified event. You can bind multiple control objects to the same event. Which means I can have 2 target with two selectors bint to the same event.
Example
At runtime all of these control messages bind into the give event will be dispatched to the appropriate target, in other words all these selector methods will be invoked on their respective target class objects.