如何以编程方式判断 IBAction 是否已被代码或用户操作调用

发布于 2024-12-01 01:17:31 字数 341 浏览 0 评论 0原文

如何以编程方式判断 IBAction 是由代码还是由用户操作调用。

例如 我有一个方法,-(IBAction)som​​eMethodWithIts:(id)sender 我已将其链接到 UISegmentedControl 上的 valueChanged 。

它可以通过以下方式调用:

  1. 用户更改段
  2. 在调用代码中设置 selectedIndex
  3. [self someMethodWithIts:foo];

有没有办法区分调用是否来自第一种方式?

干杯

萨姆

How can I tell programmatically if a IBAction has been called by code or by user action.

Eg
I have a method, -(IBAction)someMethodWithIts:(id)sender
which I have linked to a valueChanged on a UISegmentedControl.

It can be called by,

  1. User changing segment
  2. setting the selectedIndex in code
  3. calling [self someMethodWithIts:foo];

Is there a way to distinguish if the call has come from the first way?

Cheers

Sam

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

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

发布评论

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

评论(3

隱形的亼 2024-12-08 01:17:31

如果您可以将 nil 作为发送者(这是传统的)传递并使用它来指示它是以编程方式发送的,那就可以了。但我认为其他任何东西都太脆弱了,你应该像这样分解代码:

- (void)someMethod {
  // stuff shared by everyone
}

- (IBAction)someMethodWithIts:(id)sender {
  // stuff specific to IBAction
  [self someMethod];
}

如果你真的想要一个发送者,那么你可以这样做:

- (void)someMethodWithIts:(id)sender triggeredByUser:(BOOL)isUser {
}

- (IBAction)someMethodWithIts:(id)sender {
  [self someMethodWithIts:sender triggeredByUser:YES];
}

但一般来说,如果你希望 IBAction 与编程更改不同,那么不要将程序更改连接到 IBAction。

If you can pass nil as the sender (which is traditional) and use that to indicate it was sent programmatically, that's ok. But anything else I believe is too fragile and you should break up the code like this:

- (void)someMethod {
  // stuff shared by everyone
}

- (IBAction)someMethodWithIts:(id)sender {
  // stuff specific to IBAction
  [self someMethod];
}

If you really want a sender, then you can do it this way:

- (void)someMethodWithIts:(id)sender triggeredByUser:(BOOL)isUser {
}

- (IBAction)someMethodWithIts:(id)sender {
  [self someMethodWithIts:sender triggeredByUser:YES];
}

But in general, if you want the IBAction to be different than programatic changes, then don't wire programatic changes to the IBAction.

猫弦 2024-12-08 01:17:31
if ([sender isKindOfClass:[UIControl class]]) {
    UIControl *controlSender = (UIControl *)sender;
    if (sender.selected) {
        // then it's #1
    } else {
        // then it's #2
    }
} else if ([sender isKindOfClass:[Foo class]]) {
    // then it's #3
}

可能有用。或者查看 UIControl 或 UISegmentedControl 上定义的发送者的其他属性。也许状态。我的猜测是,您可以发现用户交互时和不交互时的不同之处。

if ([sender isKindOfClass:[UIControl class]]) {
    UIControl *controlSender = (UIControl *)sender;
    if (sender.selected) {
        // then it's #1
    } else {
        // then it's #2
    }
} else if ([sender isKindOfClass:[Foo class]]) {
    // then it's #3
}

Might work. Or poke around other properties of sender defined on UIControl or UISegmentedControl. Maybe state. My guess is you can find something that's different when the user is interacting vs. when they're not.

南渊 2024-12-08 01:17:31

你确实想使用这样的方法

-(IBAction)actionWithSender:(id)sender event:(UIEvent*)event
{
    if (event) {

    } else {

    }
}

如果你发现event参数是nil,那么它来自代码中的调用,否则,调用来自用户事件。

You really want to use method like this

-(IBAction)actionWithSender:(id)sender event:(UIEvent*)event
{
    if (event) {

    } else {

    }
}

If you find the event parameter is nil, it's from a call in your code, otherwise, the call is from user event.

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