I am adding observer to init method.And for the reason it will not call multiple times I am removing observer before adding it.Even then is is calling as many times as we load the View.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateStuff)
name:#"appDidBecomeActive"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(closeConnection)
name:#"appDidEnterBackground"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanges:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
t=[[Theme alloc] init];
// Custom initialization
}
return self;
}
I have also tried it removing in updateStuff method
-(void)updateStuff
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(#"Market Watch update stuff called $$$$$$$----------------------");
[self initNetworkCommunication];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
also tried removing here.
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appDidBecomeActive" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appDidEnterBackground" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
also tried removing in viewWillDisappear It works fine but here adding observer to viewWillAppear not working.
When I lock the screen and unlock it this observer should call.As it is notified on appDidBecomeActive , and it is working like that. But when I pop back to previous viewController and push to current one and repeats the process of lock and unlock this observer fires two times.As number of times I pop view and push again to current View.Notifier fires number of times I pushed to the View.I know it is because of init method.Whenever view will load it adds an observer but doesn't removes observer.
What can I do other than that.
You have to remove observer in viewWillDisappear method.
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
and remove from initWithNibName.
Try adding it to -awakeFromNib instead
Also, try removing the notification in a different manner. I don't have the code but you have to also give it the name of the notification, and you should only remove observer in dealloc
Related
I have an ARC enabled project
There are few observers added in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getSipNotification:) name:#"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(syncExtensionData:) name:#"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showLocalNotification:) name:#"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(outgoingCall:) name:#"outgoingCall" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playRingtone) name:#"playRingtone" object:nil];
I want to remove all observers so I added following line in viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self];
Now my question is, is this remove all observers?
If not how can do it?
UPDATE
If I want to remove a single observer how can do it?
Can you help me please.
Yes, It will remove all observers.
[[NSNotificationCenter defaultCenter] removeObserver:self];
And you can remove a particular observer like this...
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"syncExtensionData" object:nil];
In my application i used this notification :
for particular observer remove this way :
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
//right whetever you want
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
may be it will helpful to you.
Yes it'll remove all the observers in your class.
You can use following to remove single observer:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"getSipNotification" object:nil];
To remove individual observer.
viewDidUnload is deprecated in iOS6 and later, so Your observer never be removed from notification center in iOS6 and later. To remove single observer try
- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender
hello i assign nsnotifiaction in app delegate.m's method and this method call eprox every 30sec, and i wants its notifcation in viewcontroller adn execute method,
here is my code of appdelegate .m
- (void)layoutAnimated:(BOOL)animated{
BOOL yy= self.bannerView.bannerLoaded;
if (yy==1){
self.iAdString=[NSMutableString stringWithString:#"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionWillBegin" object:self];
}
else{
self.iAdString=[NSMutableString stringWithString:#"0"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionDidFinish" object:self];
}
}
and in viewcontroller.m
//i defined in viewdidload method
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishBannerViewActionNotification:) name:#"BannerViewActionDidFinish" object:nil];
}
its method are..
- (void)willBeginBannerViewActionNotification:(NSNotification *)notification{
[self.view addSubview:self.app.bannerView];
NSLog(#"come");
}
- (void)didFinishBannerViewActionNotification:(NSNotification *)notification {
NSLog(#"come");
[self.app.bannerView removeFromSuperview];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
I have not getting response of excessing method while method read in appdelegate file.
Please help me.
You have a typo error.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
//Your error here-------------------------------------------------------------------------------------------------------------------------------------^
You have put a space there.
SideNote: For all notification names, you should/can create a separate file and put all your notification names as constants strings.
const NSString *kBannerViewActionWillBegin=#"BannerViewActionWillBegin";
this will be easier to change the value and no such typo will happen.
From your code what I get is except for the notification name's all other stuff is fine, did you check whether the notification gets fired. Try keeping break points at the notification firing line.
In an ARC project, I addObserver for a notification in viewDidLoad: and removeObserver: in dealloc. But after I pop the viewController, dealloc is not executed.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(refreshData)
name:MyNotification
object:nil];
}
- (void)dealloc
{
NSLog(#"==================");//There is nothing print out.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MyNotification
object:nil];
}
Thanks.
this may perhaps you have enabled nszobie objects, disable them. i don't know why but if nszombie enabled in arc, dealloc is not invoked.
go to product>edit schems> unchek the enable zombie
I am trying to call a method within an uiview from AppDelegate using the NSNotificationCenter to no avail..
AppDelegate.m
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Then via MainStoryboard, the main view is loaded which controller class is MainViewController
in MainViewController.h viewDidLoad i have
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
and then the method
- (void) ProcessDidComplete:(NSNotification *)pNotification
but it never gets called.
Thanks for any help!
just change a way..
First add observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
Then Post Notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Finally remove in viewWillDisappear
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ProcessDidComplete" object:nil];
Your code looks okay, which makes me wonder where in your app delegate you post the notification?
If you post the notification before you add the observer in the view controlller, then the notification will never be received. Can you not send the message to the main view controller directly, i.e., as a property, rather than using notifications?
Just wanted to note here that iOS notification names are case-sensitive:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handUpdate:) name:#"scheduleUpdated" object:nil];
will not respond to:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScheduleUpdated" object:items];
(as I spent the last 20 minutes figuring out...)
i need to dismiss my uiimagepicker modal viewcontroller automatically when the application enters the background.i tried to put the code to dismissmodalviewcontroller code in viewdiddissappear method,but its not being called.so i made a reference to the viewcontroller in appdelegate and tried to put it in the applicationdidenterbackgroundmethod but still it is not working.can someone point out the right way to do this
Try to add an NSNotificationCenter observer for UIApplicationDidEnterBackgroundNotification in the UIViewController that you want to dismiss. Use the selector to dismiss the modalview
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)didEnterBackground:(NSNotification*)note
{
[self.navigationController dismissModalViewAnimated:NO];
}
Best way to remove the modal when app is moving to background and it works fine .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dismissView:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)dismissView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Also you can remove observer like this
[[NSNotificationCenter defaultCenter] removeObserver: self
name:UIApplicationDidEnterBackgroundNotification
object:nil];
I don't think you need to go through all that.
From the docs:
If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
Try calling [self dismissModalViewController:NO] from the parent view controller in your implementation of - (void) viewDidUnload.
This is untested, but the docs imply that it should do the job for you.