如何以编程方式触发 IBAction 方法,而不是通过按钮等?

发布于 2025-01-04 08:17:27 字数 477 浏览 3 评论 0原文

通过创建 IBAction 方法并将其连接到 IB 中的按钮,我可以轻松地“做某事”。例如...

-(IBAction)popThat:(id)sndr{ [windy setFrame:eRect display:YES];}

但是,我无法,在我的一生中,弄清楚如何通过简单的可调用方法来做到这一点...即

-(void) popThatMethod { [windy setFrame:eRect display:YES]; }

-(void) awakeFromNib  { [self popThatMethod];               }

我会 >期望此方法会执行与单击按钮相同的操作...因为它们是相同的...但是没有。什么也没发生。 我在这里缺少什么?

I can easily "do something", by creating an IBAction method, and connecting it to a button in IB. For example...

-(IBAction)popThat:(id)sndr{ [windy setFrame:eRect display:YES];}

However, I cannot, for the life of me, figure out how to do this via a simple, callable method... i.e.

-(void) popThatMethod { [windy setFrame:eRect display:YES]; }

-(void) awakeFromNib  { [self popThatMethod];               }

I would expect that this method would DO the same thing as clicking the button... as they are identical... but NO. Nothing happens. What am I missing here?

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

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

发布评论

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

评论(2

扶醉桌前 2025-01-11 08:17:27

我并没有说这绝对是最好的答案,或者是正确的方法,但一种有效的方法是将以下内容放入 -applicationDidFinishLaunching: 中:

[self performSelector: @selector(popWindow:) withObject:self afterDelay: 0.0];

这会导致它被调用运行循环的下一次传递,此时之前未到位的内容现在已就位。

I don't state that this is categorically the best answer, or the right way to do it, but one approach that works is to put the following in -applicationDidFinishLaunching::

[self performSelector: @selector(popWindow:) withObject:self afterDelay: 0.0];

This causes it to be called on the next pass of the runloop, by which time whatever was not in place before is now in place.

九歌凝 2025-01-11 08:17:27

根据您想要执行的操作,您可能希望

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

Which 触发按钮,就像它被触摸一样,并强制执行与之相关的任何操作。

注意:这是我询问 这个问题。

Depending on what you're trying to do, you might want

[buttonObj sendActionsForControlEvents: UIControlEventTouchUpInside];

Which triggers the button as if it had been touched, and forces whatever actions are connected to it.

NOTE: that's the answer from when I asked this SO question.

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