I have tried and got "SIGBRT" exception.
In Global.m, I have methodBGlobal()
.
In ClassA, I included Global.h in order to use methodGlobal()
.
ClassA.methodA()
has methodGlobal()
in it.
Then I have button in Global.m. Since I do not know how to call methodGlobal()
properly in Global.m. SO I call the methodGlobal()
through ClassA instance.
[mybutton addTarget:ClassA instance action:@selector(methodA:) ...];
It does not work. I got "SIGBR开发者_JS百科T" exception and I do not know that methodGlobal() in ClassA.methodA() was called or not? I want methodGlobal to work.
Two things you need to consider here.
- If the method is a class method, then the target should be [ClassA class].
- If the method signature doesn't include any arguments , for ex., -(void) methodA; then, there should be not colon(":") included in the @selector. So, the selector should be just @selector(methodA).
Finally the addTarget:
method should look like,
[mybutton addTarget:[ClassA class] action:@selector(methodA) ...];
References:
- Target-Action from Cocoa Application Competencies for iOS.
- The Target-Action Mechanism from Cocoa Fundamentals Guide.
you can do this by making class method like + (void) doAction
now you can use this method with the class name like [ClassName doAction]
Try this :
[mybutton addTarget:[ClassA class] action:@selector(methodA:) ...];
精彩评论