开发者

Problem understanding event handling on the iOS device

as the topic says I have a problem understanding the event handlin开发者_高级运维g.

Basically I have the following problem, I have a class that gets static calls from other classes. When such a call comes it should send a notification to other classes that listens to this.

I know there is the NSNotificationCenter, but as I see it, this works somehow the other way arround ?

Hope someone can help me out here.

-mcb


NSNotificationCenter is super easy to use.

One class says:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserverForName:@"UniqueNotificationName" 
                    object:self 
                     queue:nil 
                usingBlock:^{
    NSLog(@"I just got notified of UniqueNotificationName!");
    //whatever else you want to do when you receive this notification
}];

Then another class says:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"UniqueNotificationName" 
                      object:self];

And ta-da! You've notified the first class that the second one fired a notification. Easy peasy.

If blocks intimidate you (they intimidated me, before I started really working with them), there's also a version of "addObserver" that takes a selector instead. Go look a the docs for how to spell that. IMO the class docs for NSNotificationCenter go in depth into the workings of the thing in a way that's unnecessary for most users, so don't worry if you go in there and it seems harder than it ought to.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜