send application delegate message to current view without notification center - iphone

I want to display application delegate message such as "Application became active" (This is called when -applicationDidBecomeActive:application is called)
on Window.
One way is to use notification center like below:
AppDelegate.m
NSNotification *n = [NSNotification notificationWithName:#"AppBecameActive" object:self];
[[NSNotificationCenter defaultCenter] postNotification:n];
ViewController.m
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(showMessageAppBecameActive) name:#"AppBecameActive" object:nil];
This way is only way to show application delegate message ? Or, is there any other way such as property to look current view controller instance ?
Thank you for your kindness.

If you have access to the ViewController from your appDelegate. (I mean like #property or instance is reside in it) you can straight away send a message. If you do not have such access to it. write key value observer for a single ton and let your viewController receive the change.

Register below code in your viewController which wants notification.
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];
iOS framework posts a notification when your app become active, if you will register via above way, you can handle the notification in registered method (In this case yourMethod).

Related

Execute code after method has finished

I need to execute some code after I know the keyboard is hidden.
Ive been looking in to blocks but I'm just not understanding how they work enough to do this...
All I want to do is run [self hidekeyboard] then when that is complete (and the keyboard fully hidden) then I want to call a delegate.
What is the best way to handle this and how?
You want to use the UIKeyboardDidHide notification and run your code in there. Here is the link in the docs...
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(onKeyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
And the onKeyboardDidHide:
-(void)onKeyboardDidHide:(NSNotification *)notification
{
// execute what you want.
}
Register a listener for the UIKeyboardDidHideNotification using the NSNotificationCenter class.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardHidden:)
name:UIKeyboardDidHideNorification
object:nil];
- (void)keyboardHidden:(NSNotification *)notif
{
// do stuff
}
(Don't forget to remove the observer in - dealloc so that no messages will erroneously be sent to deallocated objects.)
You probably want to register to receive notifications of UIKeyboardDidHideNotification.
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

How to get a pointer to a viewController inside the appDelegate when using storyboards

I'm trying to save the data from a viewController. I'm doing this by using the delegate methods in the appDelegate: - (void)applicationDidEnterBackground:(UIApplication *)application
The problem is when using storyboards, the viewControllers automatically get set up for you and I'm not sure how to get a pointer to them so that I can access their data for saving.
How can I get a pointer to them in appDelegate while using storyboards?
You can register to receive going to and from background notification inside your UIViewController and manage the saving there.
//Going into background
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(saveData) name:#"UIApplicationDidEnterBackgroundNotification" object:nil];
//Waking up
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doSomethingOnWakeup) name:#"UIApplicationWillEnterForegroundNotification" object:nil];

Performing an action on one view, and the action doing something on another view? Is this possible?

I have 2 views:
OneViewController
TwoViewController
TwoViewController has an IBAction which plays a sound. Once the user has pressed the button on TWoViewController I want a UILabel which will appear on OneViewController saying that the sound has been played.
Thanks for the help
All you have to do is reference one viewController in the other one, that way you can call it's methods. Or you can simply create a delegate.
One possible solution is to use notifications.
In the action that plays a sound, post a notification to the default notification center that indicates the sound has played.
[[NSNotificationCenter defaultCenter] postNotificationName:"playSoundNotification"
object:self
userInfo:nil];
When OneViewController is created, have it register for the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(showPlayedLabel:)
name:"playSoundNotification"
object:nil];
When it receives the notification -- in showPlayedLabel: -- display the UILabel. Note that showPlayedLabel must follow the appropriate signature format.
- (void) showPlayedLabel:(NSNotification*) aNotification;

How to monitor NSSystemClockDidChangeNotification in iPhone SDK

I Need to know if the user change the system time in the springboard settings.so my background program can be notified. According to doc, NSSystemClockDidChangeNotification is the one choice.
but I can't receiving anything by this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleSysTimeChanged:)
name:NSSystemClockDidChangeNotification
object:nil];
Where am I wrong? Any suggestions?
Your handler method must be implemented in the same class you add as observer and needs to look like this:
-(void) handleSysTimeChanged: (NSNotification*) notification
{
// ...
}

NSNotification in iphone

i am sending NSSNotifcation to another view controller in iPhone app but its observer method getting notified two times how its possible can any one guide me
i have use this code to post notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateStatusOnFacebook" object:nil userInfo:nil];
and added observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
Have you added the observer twice?
Which method are you calling addObserver:selector:object: in? If it's in viewWillAppear then this might be called more than once.
Your method will be called the same number of times that you have added an observer.
Try this:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"updateStatusOnFacebook" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(postToWall) name:#"updateStatusOnFacebook" object:nil];
The other reason is that you might just be sending the notification twice :)
I had the same problem crop up, and read this question, but could only find the one call to add the observer anywhere in the project.
In our case, the observer was being added twice because the method the line was in was being called twice.
Make sure you step through your code, breaking on your addObserver:selector:name:object call, to ensure that you don't have an unexpected extra execution path to that call.