xcode 如何以编程方式与通过按钮等方式触发 IBAction 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9219610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger an IBAction method programmatically vs. via a button, etc?
提问by Alex Gray
I can easily "do something", by creating an IBAction method, and connecting it to a button in IB. For example...
通过创建 IBAction 方法并将其连接到 IB 中的按钮,我可以轻松地“做某事”。例如...
-(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 expectthat this method would DO the same thing as clicking the button... as they are identical... but NO. Nothing happens. What am I missing here?
我希望此方法与单击按钮的作用相同……因为它们是相同的……但是 NO。没发生什么事。 我在这里错过了什么?
采纳答案by ipmcc
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:
:
我没有说这绝对是最好的答案,或者正确的方法,但一种有效的方法是将以下内容放入-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.
这会导致它在 runloop 的下一次通过时被调用,到那时之前没有的任何东西现在都已经到位。
回答by Olie
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.