Call “(id)sender” method in Xcode

ε祈祈猫儿з 提交于 2019-12-21 02:46:29

问题


Here is the method I wish to call:

- (void)myMethod:(id)sender {

How would I call it? I tried:

[self myMethod]

^error: Expected Expression before "]" token.

I know this is a simple question, but I am new to iPhone Development


回答1:


The method takes one parameter, so you have to give it one. If you have no sender you want to give, just pass nil:

[self myMethod:nil];

You could also overload the method as a convenience:

// declarations
- (void)myMethod;
- (void)myMethod:(id)sender;

// implementations
- (void)myMethod { [self myMethod:nil]; }
- (void)myMethod:(id)sender { /* do things */ }



回答2:


Unless you wanted to send an unspecified-type object to your method you don't need the (id)sender portion:

- (void)myMethod {

}



回答3:


You need to pass along a sender when you call it.

[self myMethod:something]

In other words, you need to have an argument to pass along when you call the method.



来源:https://stackoverflow.com/questions/4456115/call-idsender-method-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!