开发者

Objective-C : Call a method from another class in addTarget:action:forControlEvents

Here is my problem :

I want to assign an Event on an UIButton. I use this method :


- (void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents

like this :


[newsButton addTarget:self
action:@selector(myEvent2)
forControlEvents:UIControlEventTouchUpInsi开发者_如何学Pythonde];

newsButton is my UIButton.

If myEvent2 belongs to the class where I am, the code is compiling and executing fine, everybody is happy.

But if myEvent2 belongs to another class I don't succeed to execute it (the project compiles fine).

I tried out to change my code in that way :


MyViewController* test = [[MyViewController alloc] init];
[newsButton addTarget:self
action:@selector([test myEvent2])
forControlEvents:UIControlEventTouchUpInside];

but I get the followings errors :

Expected ':' before '[' token

Method name missing in @selector

Does someone have any solution to my problem ?

Thanks by advance :)


You need to change addTarget:self to addTarget:test


MyViewController* test = [[MyViewController alloc] init];
[newsButton addTarget:test
    action:@selector(myEvent2)
    forControlEvents:UIControlEventTouchUpInside];


Another option is to use a redirection method:

- (void)myRedirectHandler {
    [ test myEvent2 ];
}

...

[newsButton addTarget:self
    action:@selector(myRedirectHandler)
    forControlEvents:UIControlEventTouchUpInside];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜