use selector to call function
Why does the following code not work?
[Confirm addTarget:self
action:@selector([Func开发者_JAVA百科tionsApp Synch_Data:CompIDS])
forControlEvents:UIControlEventTouchUpInside];
given:
FunctionsApp: its other class
Synch_Data:its function in FunctionsApp
CompIDS: its MSmutableArray
FunctionsApp *functions = [[FunctionsApp alloc] init];//of course u need to use your own init method
[Confirm addTarget:functions action:@selector(Synch_Data) forControlEvents:UIControlEventTouchUpInside];
the selector u pass is just the name of the method this is passed to the target who in turn checks if it exists and then calls it. u cant use a mutable array as a selector. since confirm
is a button u should use a method with signature - (void)buttonClicked:(id)sender
where sender will be the button. and also since the selector is not a class-method you do not call it like this [FunctionsApp buttonClicked:]
but instead you use the FunctionsApp object reference as target ( i.e. where the method exists as target).
and please elaborate your question next time and be a bit more concrete in what things are.
Let's take this one thing at a time.
1.
-[UIControl addTarget:action:forControlEvents:]
has specific requirements to what the action
selector can look like.
It can be one of the following:
- (void)action;
- (void)action:(id)sender;
- (void)action:(id)sender forEvent:(UIEvent *)event;
Normally you'd use the second one.
2.
@selector
takes the name of the method (selector) you'd like to have called on the target. You're specifying the return value of a method call. That's the error, you're seeing.
So to call Sync_Data:
(which in objective-c would be syncData:
) on FunctionsApp
you'd write
[Confirm addTarget:FunctionsApp action:@selector(Synch_Data:) forControlEvents:UIControlEventTouchUpInside];
You still can't pass the CompIDS
variable as a parameter, though, so I'd suggest rewriting your code, to make it an instance variable, that your action method can access.
精彩评论