将按钮的目标和选择器设置为多种方法

发布于 2025-01-01 08:33:56 字数 446 浏览 0 评论 0原文

我想添加多种方法作为按下按钮时的选择器响应。一个按钮可以有两个在按下按钮时调用的方法吗?

通过研究,我发现,在 Objective-C 编程语言指南中,按钮将调用与选择器同名的所有方法。

我希望我的按钮同时执行两个操作:

  1. 播放音频文件
  2. 在数组中显示视图。

    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:

  1. play the audio file
  2. display views in array.

    UIBarButtonItem *play = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
    target:self
    action:@selector(play:)];

Appreciate advice.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放我走吧 2025-01-08 08:33:56

@selector() 字面上只是返回一个 SEL 值,它只是一个名称(事实上,在幕后,它实际上是一个字符串)。它没有指定任何特定行为。类选择在收到选择器时如何响应。

您当然可以让一个类实现一个执行两件事的方法,并将该方法的选择器设置为控件的操作:

- (void)eatCakeAndIceCream {
    [self eatCake];
    [self eatIceCream];
}

您还可以通过重复调用 addTarget:action:forControlEvents:addTarget:action:forControlEvents::

[someControl addTarget:self action:@selector(eatCake) forControlEvents:UIControlEventTouchDown];
[someControl addTarget:self action:@selector(eatIceCream) forControlEvents:UIControlEventTouchDown];

@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:

- (void)eatCakeAndIceCream {
    [self eatCake];
    [self eatIceCream];
}

You can also add multiple actions to a control with repeated calls of addTarget:action:forControlEvents::

[someControl addTarget:self action:@selector(eatCake) forControlEvents:UIControlEventTouchDown];
[someControl addTarget:self action:@selector(eatIceCream) forControlEvents:UIControlEventTouchDown];
孤独患者 2025-01-08 08:33:56

您可以为特定事件指定多个目标操作对。

[btn addTarget:self action:@selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(displayViews:) forControlEvents:UIControlEventTouchUpInside];

You can specify multiple target-action pairs for a particular event.

[btn addTarget:self action:@selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(displayViews:) forControlEvents:UIControlEventTouchUpInside];
下雨或天晴 2025-01-08 08:33:56

每次向对象添加目标时,它都会创建一个控制对象(也称为“操作消息”)(控制对象是 UI 控件类型)。该控制对象包含称为“操作选择器”的选择器的名称以及需要调用该选择器的目标。然后,该控制对象与指定事件绑定(注册)。您可以将多个控制对象绑定到同一事件。这意味着我可以将 2 个目标和两个选择器绑定到同一事件。

示例

[btn addTarget:oneTarget action:@selector(foo:) forControlEvents:UIControlEventTouchUpInside];

[btn addTarget:twoTarget action:@selector(bar:) forControlEvents:UIControlEventTouchUpInside];

在运行时,绑定到给定事件的所有这些控制消息将被分派到适当的目标,换句话说,所有这些选择器方法将在其各自的目标类对象上调用。

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

[btn addTarget:oneTarget action:@selector(foo:) forControlEvents:UIControlEventTouchUpInside];

[btn addTarget:twoTarget action:@selector(bar:) forControlEvents:UIControlEventTouchUpInside];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文