How to make custom notification so that whenever event occurs it will handle in iOS - iphone

Hi I want to make custom local notification so that whenever popover is visible it will handle by that notification.
so that as multiple time the popover is visible it will handle by that notification
Currently i have partially done this but problem is that if i want to run notification's selector method multiple times i have to post that notification wherever i want.
used this link to implement it
i want to send some notifications to observers when some event occurs. and i also want to know how observer catch/handle/receive that notification?
Can i make notification like, once i postNotification in viewDidLoad it will handle as many times that event occurs?
Note-See the answer posted by me

Use this
line before presenting popover
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dissmissPop:) name:#"popOver" object:popOver.contentViewController];//popOver is your name of popover
-(void)dissmissPop:(id)sender{
//method to be called
}
To call notification from popover use this
[[NSNotificationCenter defaultCenter] postNotificationName:#"popOver" object:self];

Notifications are same as broadcast receivers. If we register as broadcast receiver whenever new email arrives. But do you actually post the event notification? No. Someone else does. Similarly when keyboard is shown the notification is posted by the system. Send you only get notified.
The system posts whenever the keyboard is gonna appear. This should make it clear that if you want to post custom notifications you have to post it every time there's need for it to.
So if you make custom notification you have to postNotification when ever you want to post it.

Related

how to use NSNotification in objective c

I don't know how to use NSNotification in our iphone application. and one more doubt of difference between delegate and NSNotification because both are communicating through objects.
and give the practical example.
=> NSNotificationCenter provides a centralized hub through which any part of an application may notify and be notified of changes from any other part of the application.
=> Observers register with a notification center to respond to particular events with a specified action.
=> Each time an event occurs, the notification goes through its dispatch table, and messages any registered observers for that event.
Use Of NS-Notification in Objective C
//Write from where you want to pass the data
[[NSNotificationCenter defaultCenter]postNotificationName:#"TeamTable" object:hdImage userInfo:nil];
Here
**TeamTable is notification observer name (Unique name)
**hdImage is what data you want to pass to another controller
Now write these code in that Controller from where you want to receive
the data
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(detailsData:) name:#"TeamTable" object:nil];
}
-(void)detailsData:(NSNotification*)sender{
//In sender it contain All received data
}
It’s important for objects to remove observers before they’re deallocated, in order to prevent further messages from being sent.
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"TeamTable" object:nil];
}
For More Details about NS-Notification You can follow this Link http://nshipster.com/nsnotification-and-nsnotificationcenter/

Local notifications will not go away

Yesterday i added local notifications to my app (including repeating ones) and then quite often when i launch the app it pops up the notification. I commented out all the code referring to the local notifications and it's still doing it. I deleted the app from the simulator too, then re-installed it, and it's still doing this. Any suggestions?
EDIT: It actually appears that it keeps firing the notification once a minute, for some reason.
If you mean UILocalNotification, then add this line to your app and run once
[[UIApplication sharedApplication] cancelAllLocalNotifications];
That will cancel all local notifications your app ever added, including those in prior runs. Related, you can check the scheduledLocalNotifications property of UIApplication as well.
If you haven't added a [[NSNotificationCenter defaultCenter] removeObserver:self] in any view controller that potentially receives a notification and then you pop that view controller off the navigation stack, because it is retained by the notification center, it stays in memory and will continue to respond when the notification comes. I understand you've said you removed the postNotification code, but you must have missed some code somewhere as this can't happen automatically without something calling post notification. What is the message you are receiving? Is it an alert view that pops up? Does it have a custom message or a system message? Find the place where that alert view is displaying and make sure you remove that view controller from the notification center. If it is a system message, figure out what view controller is setup to receive notifications for that system message and make sure *it gets properly removed as an observer from the notification center.
Best regards.

Responding to EKEventStoreChangedNotification

I'm trying to listen for EKEventStoreChangedNotification to check if the calendar is changed while my app is in background.
I register the observer in a view controller's initWithNibMethod like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(calendarChanged:) name:EKEventStoreChangedNotification object:nil];
The calendarChanged method just logs a message on the console to check if it's called.
Problem is my observer method never gets called (the observer object is still valid). From what I understand, unless an app is registered to do background execution (my app is not set up for this) the notifications of that type should be coalesced and delivered on entering foreground.
I think the "object:" needs to set with the EventStore object you're using.
Yes, you won't get called whilst you're in the background but your "calendarChanged:" selector will be called when your app' comes in to foreground.

NSNotification in iphone sdk

what is the use of NSNotification in iphone sdk?
Thanks
I know this isn't generally a good way to answer questions, but RTFM.
NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
You can create a notification object with the class methods notificationWithName:object: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.
NSNotifications allow you to have a method called when a event occurs.
For example if you have a MPMoviePlayer and you want todo something when it is done you could use the following code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:yourMoviePlayer.moviePlayer];
or if you want to do something when the device rotates:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:self];
You should have a look at the Notifications section in the Cocoa Fundamentals Guide. There's anything you need to know about notifications there : from definition to use cases.
In terms of events NSNotifications are an alternative to delegation. Delegation can be used to notify one single delegate of an event, whereas notifications can be used to notify an arbitrary number of receivers. A notification is sent to the main notification center, which then notifies every object, that has registered for the notification.
One important difference is, that with delegation you can receive the delegates response to the event, whereas with NSNotifications you just send away the notification, but you don't know about the receivers or their response to the notification.
You register a UINotification when you want to receive a alert from iOS. So if you want to do something when a accessory is plugged in or a TV is plugged in you would register a UINotification for it and it would call a method in your app when the event occurs.

How to specify notificationSender? (Cocoa Touch)

All of the examples I can find of setting up a notification look like this:
[nc addObserver:self selector:#selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
where object is always being set to nil. object is the notificationSender, which is defined thusly in the documentation:
"The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer."
I want to receive the keyboard notification only for one particular text view, so I want to tell addObserver that, but I don't know what to give it for the object. I tried the outlet variable that's bound to the text view I'm interested in, but that just resulted in my getting no notifications at all.
Any suggestions?
The UIKeyboardWillShowNotification is coming from your window instance, and for all intents and purposes is a "system" notification. The keyboard is either showing or not showing, it's not really something that is tied to a specific control.
If you want to do something when a user enters a specific text field, you should probably control that in the text field's delegate instead.
In this case you can not get the notification only for one particular text view, as the docu for UIKeyboardWillShowNotification says "The notification object is nil.".
You have to check in your keyboardWillShow impl if your particular text view isFirstResponder.
I had two competing needs - I had to use the keyboard notification because I needed to get the keyboard height, which appears to be only available that way, but I also needed to know which text view I was in, which meant also using the textViewDidBeginEditing delegate. After much messing around I finally resorted to getting the keyboard height in the notification method and storing it in an instance variable, which was then available to use in the delegate method (I need to scroll the view up so the bottom text view is not mostly hidden under they keyboard when they start typing). A bit inelegant, but it works.
Thanks for the pointers!