开发者

Objective-C: How Can Class call global method safely?

开发者 https://www.devze.com 2023-03-16 01:43 出处:网络
I have tried and got \"SIGBRT\" exception. In Global.m, I have methodBGlobal(). In ClassA, I included Global.h in order to use methodGlobal().

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.

  1. If the method is a class method, then the target should be [ClassA class].
  2. 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:

  1. Target-Action from Cocoa Application Competencies for iOS.
  2. 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:) ...];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号