开发者

Button's target is always "self"? Can I set to be another?

Is it possible or not to set button's target to be another thing t开发者_运维百科han "self"? Such as UIView or UIWindow??

I tried but I got EXC_BAD_ACCESS as error.


You can set the target to any instance/object. The instance/object should respond to the selector you set. Otherwise you will get the SIGABRT exception.

Mostly we set the target to our custom view controllers, because we do some operations on some events. We set Self as target if we defined the action in the current view controller. You can set it some other view controller like the following,

AViewController *a = [[AViewController alloc] init];
[yourButton addTarget:a action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

When you click on the yourButton the action onButtonPressed: of the view controller instance a will be triggered. Note: AViewController should implement the method onButtonPressed:. You will receive SIGABRT, if not.


I had the same errors, it resolved with the change of [AViewController class] as target.

[yourButton addTarget:[AViewController class] action:@selector(onButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

My class is also declared as + sign, because its a library to use in all apps.

@interface SocialClass : NSObject

+ (void) gotoYoutube;
+ (void) gotoFacebook;
+ (void) gotoTwiter;

@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜