在Xcode中调用"(id)sender"方法

Lin*_*int 3 methods objective-c

这是我想要调用的方法:

- (void)myMethod:(id)sender {
Run Code Online (Sandbox Code Playgroud)

我怎么称呼它?我试过了:

[self myMethod]
Run Code Online (Sandbox Code Playgroud)

^ 错误:"]"标记之前的预期表达式.

我知道这是一个简单的问题,但我是iPhone开发新手

Jas*_*oco 12

该方法采用一个参数,因此您必须给它一个.如果您没有要发送的发件人,只需传递nil:

[self myMethod:nil];

为方便起见,您还可以重载该方法:

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

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