views:

740

answers:

4

Hi, 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];
}
+3  A: 

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?

cobbal
ok. so if i need that that my ViewController will get that notification for example after a call entered and abandoned and the app return the app need to call this method, where will i put the NSNotificationCenter to call the method?
omri
you should probably put the call inside the `initWithNibName:bundle:` if you need it inside your ViewController.
cobbal
Since this is an iPhone application, I think viewDidLoad: might be a better place for it, and then unregister in the viewDidUnload:. Normally you only want a view to respond to the UIApplicationDidBecomeActiveNotification only if it was the active view at the time of the interruption
Brandon Bodnár
@bodnarbm that's probably better if it's a root view controller; it could cause some issues if the view is lazy loaded.
cobbal
+1  A: 

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.

stefanB
+1  A: 

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.

Kendall Helmstetter Gelner
A: 

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.

Girish Kolari