so in my viewdidload i got something like
[nameTextField becomeFirstResponder]
now after a button gets klicked, i want to slide out the keyboard of this textfield, and slide another keyboard of another textfield in.
i thought about
[nameTextField resignFirstResponder];
[dateTextField becomeFirstResponder];
but the other keyboard shows up immediately.
commenting the [dateTextField becomeFirstResponder]; out, effects that my nameTextField keyboard slides out as i wanted.
any ideas how to do this?
thanks!
Is there a reason why you want to do this? It will obviously increase the time it takes for a user to enter information, which I know would bug me.
But if you do really want this effect, then I would look at something like this:
[dateTextField performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:timeDelay];
Where timeDelay is the amount of time it takes to dismiss the first keyboard.
You can register to observe these notifications: UIKeyboardWillShowNotification , UIKeyboardWillHideNotification. That will let you keep track of what is going on, but it can easily get pretty complicated so Tom Irving's suggestion might be easier to work with.
To get notifications on keyboard hiding and showing, have a look at
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification object:[self view].window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification object:[self view].window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:[self view].window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:[self view].window];
and add appropriate methods like
-(void)keyboardWillShow:(NSNotification*)notif
-(void)keyboardWillHide:(NSNotification*)notif
-(void)keyboardDidShow:(NSNotification*)notif
-(void)keyboardDidHide:(NSNotification*)notif
Then you can connect the animations any way you like.
Be sure to NSLog() all of them, they are not always called when you would expect them (the notorious one being when you go from one field to another, and you receive the willhide and willshow immediately)
Related
My controller main view call
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
in it's viewDidAppearmethod because the screen must be manually rotated for the user.
Everything works perfectly except when I put the app in background and I re-enter foreground on this screen: Then the navigationBar's height become 32px.
If I comment the setStatusBarOrientation call, then no problem.
I've logged the navigationBar height in didEnterForeground method (after the super call), but it tells 44px. So I guess it would be resized after.
So I would like to know :
If there was a way to prevent the navigationBar to be resized
If no, what other callback method would come after the didEnterForeground one (viewWill/DidAppear does'nt)
Thanks !
After 4 months, I finally found a partial solution !
If no, what other callback method would come after the didEnterForeground one (viewWill/DidAppear does'nt)
I found a good callback when a viewController (not app delegate) is re-entering foreground. With NSnotificationCenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
don't forget to remove
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
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.
Is it possible to disable the keyboard from showing when a webpage element is selected inside a UIWebView?
Register for a keyboard notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
And then you can stop it from showing by placing the following code in keyboardWillShow :
UITextField *dummyTextField = etc.
//Basically, create a dummy uitextfield that you never show.
//I can't remember all the syntax :)
[dummyTextField becomeFirstResponder];
[dummyTextField resignFirstResponder];
//Keyboard should be gone. Hoorah!
Not sure 100% if this will work without flaws. If the keyboard starts to animate then hides itself again, you could use
[UIView enableAnimations:NO];
If the keyboard doesn't like being resigned while it's showing then you could try changing the inputView property of the dummy textField to some dummy UIView instead.
Hopefully this should get you somewhere!
I have some problem. On start of my application I automatically show number pad keyboard(I send becomeFirstResponder to one of textfields) . But if user has used emoji keyboard before my application, emoji keyboard is shown. How can I avoid it? I want to show exactly number pad, but not emoji keyboard.
Use the keyboard notification and track the keyboard by using some BOOL variables,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
Check if a keyboard is already there by using the BOOL var. If its there resign it and show your keyboard.
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.