开发者

@selector to a function in a separate class

If I have a function that is defined in class Foo, and my class Bar has a reference to class Foo, can I use the @selector to call that funct开发者_Python百科ion? Something like

@selector([Foo someFunctionInFoo:])

I have only used the @selector to call functions in the same class. I tried doing something similar to the above code, but it didn't work and I wasn't sure if it was even possible. Thanks.


@selector doesn't store any calls information, just the selector.

[Foo performSelector:@selector(someFunctionInFoo:) withObject:nil];

You can also do:

Class class = SomeClass;
id obj = [class someObject];
SEL sel = @selector(someFunction);
[class performSelector:sel];
[obj performSelector:sel];

As long as both implement someFunction.

To specify which target is used by buttons etc you set the target and the action as two properties, e.g.:

[[UIBarButtonItem alloc] initWithTitle:@"Filter"
                                 style:UIBarButtonItemStyleBordered
                                target:foo // The object to send the message to
                                action:@selector(someFunctionInFoo:)]; // The message to send
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜