omri
omri

Reputation: 305

Objective-C - where to put the NSNotificationCenter?

i have an NSNotificationCenter selector,

where to put it ? in the delegate (if yes then where?) in the controller?

where to put the method as well.

do i need to dealloc the NSNotificationCenter ?

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceNotificationReceived:) name:UIApplicationDidBecomeActiveNotification object:nil];



- (void)deviceNotificationReceived:(NSNotification *)notification
{
    [self.soundMgr endInterruption];
}

Upvotes: 0

Views: 3872

Answers (4)

Girish Kolari
Girish Kolari

Reputation: 2515

where to put it ?

It depend on when you need to register for notification. One way is to add observer in 'init' method of the class and remove notification in 'dealloc'method of the class.

Upvotes: 0

Since you are subscribing to the UIApplicationDidBecomeActiveNotification notification, the most logical place to put the notification is in the applicationdDidFinishLaunching method of your app delegate.

That's the first point your code gets called, so you cannot set it earlier.

Upvotes: 1

stefanB
stefanB

Reputation: 79810

Hi, i have an NSNotificationCenter selector,

okay, you mean you have a selector for a method in NSNotificationCenter.

In Objective-C, “selector” has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. http://developer.apple.com/mac/library/documentation/cocoa/....../ocSelectors.html

So you have created a selector that refer to a method.

where to put it ?

It's a variable, you can store it where ever you feel it fits in your design.

in the delegate

See above.

(if yes then where?)

It's a variable, it depends on your usage.

in the controller?

Do you have controller? Depends on your design.

where to put the method as well.

Which method?

do i need to dealloc the NSNotificationCenter ?

No, [NSNotificationCenter defaultCenter] returns a reference to the notification center, you don't dealloc it.

Upvotes: 2

cobbal
cobbal

Reputation: 70743

The deviceNotificationReceived: method must be an instance method of the argument to addObserver:. It is self in this instance, so your method should go in the same class.

You should not release the NotificationCenter, as you did not create or retain it.

Your question was a little hard to understand, is this what you were asking?

Upvotes: 3

Related Questions