Execute code after method has finished - iphone

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

Related

In terms of iphone application, the way to reload displayed page automatically when a background app comes back to be active

I have developed an iphone application with Phonegap/Cordova v1.9.0.
I want to realize the following matter.
-When a background app comes back to be active(When the app icon is tapped), a displayed page is reloaded automatically-
Probably I should make some programs in a function, (void)applicationDidBecomeActive, in Appdelegate.m or MainViewController.m, but I have no idea what to do.
Please tell me how to solve this case.
You can use the NSNotification observer pattern. In your MainViewController.m file, and viewDidLoad, you can add an observer (registering for notifications):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
Then you must implement appDidBecomeActive: (you can give the selector any name, but you must implement a method of that name). In this example:
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(#"App became active");
}
When the app is resumed, and should this view controller be active, it will simply log that to the console. You can put any code you wish inside that method (in your case, refreshing a page).
Don't forget to remove the observer when the view controller is deallocated in the dealloc method. This will remove all observers for you.
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSLog(#"Removed all notification observers");
}
Are you really using version 1.9?
Try this:
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event, reload the page or content
}

send application delegate message to current view without notification center

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).

Which is a better way to remove Notification observer

I usually use NSNotification like the sample below:
In viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(foo:) name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bar:) name:kName2 object:nil];
In viewDidUnload and dealloc:
[[NSNotificationCenter defaultCenter] removeObserver:self];
But a friend told me that I should not use [[NSNotificationCenter defaultCenter] removeObserver:self]; because it will remove all the observers including the super class's . He suggested me to use the following code to remove observer one by one.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName2 object:nil];
I've checked the ASIHttpRequest library's code ( https://github.com/pokeb/asi-http-request ). It follows my friends' suggestion.
I want to know if my friend is right or not? In my opinion, since the current instance will be unload or dealloc, the super class's notification is also useless. And is there any system UIViewController subclass use notification?
Your friend is 100% correct. Though, it does not matter if you remove all notification observations in dealloc.
You mentioned viewDidUnload, and there the case is completely different, because the unloaded object will stay alive, and you don't know when the notification observations of the superclass are added again. If they are added in viewDidLoad you won't have a problem. If they are added in an init method you just lost a bunch of important notification observations.
Removing observations with specific names is good practice and should be done from the beginning.
When you want to remove all notification you use,
[[NSNotificationCenter defaultCenter] removeObserver:self];
If you want to remove a particular notification you use,
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName1 object:nil];
When you no longer in need of any notification the first approach is simple.
As the object is going away, it's safe to use [[NSNotificationCenter defaultCenter] removeObserver:self]; in the dealloc method.
In ViewDidUnload method, you'd better remove each observer one by one as a reference to the controller is still around (and your corresponding viewDidLoad should add them all back in).
I use the first way, I never thought about whether it was right or not. If dealloc is being called, then the object (super as well) is going to be deallocated anyway. What you definitely DON'T want is the NSNotification being sent to a deallocated instance.

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.