I added following code to viewWillAppear:animated in main view controller.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
And, I implemented this method in same class,
- (void)showKeyboard:(NSNotification *)notification
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Keyboard will appear." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Main view controller has a UITextField object.
In iPad2 (iOS 5.0), an alert view appears when it was focused.
However, in iPad mini (iOS 6.0), nothing appears except a software keyboard.
I want to make iPad mini be same behavior as iPad2.
Thanks,
As of iOS 3.2, UIKeyboardWillHideNotification and UIKeyboardWillShowNotification are no longer fired when switching between two text fields. Basically, the notifications only fire if the keyboard is actually shown or hidden. Use UIKeyboardDidShowNotification instead.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
Related
From past few days I am facing a weird keyboard issue that is only happening in iPhone 5c only.
I am using objective-C for Development in Xcode-6.4
My environment target is ios7.
Here is How I am handeling keyboard Notification.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
To Deregister Notification I am writing this piece of code.To be sure I use -resignFirstResponder for every textField.
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self hideKeyBoard];
[self.view endEditing:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)hideKeyBoard{
[kAgeTextField resignFirstResponder];
[kSchoolTextField resignFirstResponder];
}
And In submit button I have checked some condition and then I am showing an AlertView.
- (IBAction)submitClicked:(id)sender
{
if(validated)
{
[self.view endEditing:YES];
[self hideKeyBoard];
[self.view resignFirstResponder];
[self makeApiCall];
}
}
Now when I get Success/Failure Response from server I am doing this.This is the block which runs after getting response from server:
-(void)SuccessfulWithServerInfo:(id)responseInfo
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
dispatch_async(dispatch_get_main_queue(),^{
[appDelegate hideProgressViewFromView:self.view];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:#"Thanks for coming" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:YES];
});
}
Problem When I get alertBox and press ok. Then keyboard opens up and closes automatically. This is happening i iPhone 5C only. I checked it in 4s,5s,6 and 6Plus. All are working fine.
If anyone knows about it please assist.
You are displaying alert at same time you are also doing popToRootViewController. May be this will cause problem.
Display alert.
Handle alert view method.
Write [self.navigationController popToRootViewControllerAnimated:YES] in alert view's method.
[UIAlertView showWithTitle:#"" message:#"Thanks for coming" cancelButtonTitle:#"OK" otherButtonTitles:nil] alertViewStyle:UIAlertViewStyleDefault tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex, NSString *text)
{
if(buttonIndex == 1)
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
Hope this will help you.
After Some Research I found this answer In stackOverflow.
Their is some change in the behaviour of AlertView in ios7 and in ios8.
I use this code to solve my issue:
[self performSelector:#selector(showAlertView) withObject:nil afterDelay:0.6];
For details answer please refer to this SO answer
My view controller registers to keyboard notifications (keyboardWillShow, keyboardWillHide).
I launch my app. It is showing the viewcontroller that is registered to keyboard notifications. The keyboard is not visible.
I switch to the sms app and start writing text. While I'm writing, my app gets a notification. The notification is displayed as a banner on the top of the screen.
When I click the banner, my app is opened and immediately gets a keyboard notification.
As far as I can tell, this keyboard notification is related to the keyboard of the SMS.
How do I identify if the keyboard event came from my app or not?
you can remove listening to observers (keyboard notifications) in viewWillDisappear and can start listening to observer again in viewWillAppear, this might solve the problem
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterBackground:)
name:UIApplicationWillResignActiveNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillDisappear:animated];
[self registerForKeyboardNotifications];
}
- (void)deregisterForKeyboardNotifications {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[center removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self deregisterForKeyboardNotifications];
}
So I've got a UIActivityViewController that takes up half the screen when my app runs, what happens is when I click the half it doesn't take up then it'll disappear and my original blank ViewController appears.
Is it possible to permanently keep the UIActivityView up until the user clicks on one of the sharing options (such as Facebook, or Mail)? Otherwise I'll just work around it.
I load the ActivityView with:
-(void)viewDidAppear:(BOOL)animated{
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:#[#""] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
}
Yes, it is possible, you can try something like
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showAViewController) name:#"showAViewController" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dissmissAViewController) name:#"dissmissAViewController" object:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"showAViewController" object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"dissmissAViewController" object:nil];
}
-(void)showAViewController{
self.view.userInteractionEnabled=NO;
}
-(void)dissmissAViewController{
self.view.userInteractionEnabled=YES;
}
When you are showing a view controller on top of another view controller just call
[[NSNotificationCenter defaultCenter] postNotificationName:#"showAViewController" object:nil];
After you dismiss it or remove it, just call:-
[[NSNotificationCenter defaultCenter] postNotificationName:#"dissmissAViewController" object:nil];
Init section:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
Some method:
- (void) keyboardWillShow:(NSNotification*) aNotification {
// TO DO
}
Dealloc section:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
So keyboardWillShow is not called after presenting view in iOS 6.1...
In iOS 6.0 this code works perfectly.
In which "init section" are the observers being added? If your view controller is from a storyboard, for example, then it should be in - (id)initWithCoder:(NSCoder *)decoder.
My recommendation, however, is to set up the observers in viewWillAppear and remove them in viewWillDisappear. That way the setup and tear down are "balanced" and only active when the view controller's content is visible.
I got a UITextField, when you click on it there pops up a keyboard. I've added the UITextField using storyboard and now I want to disable a button when the keyboard pops up. How can I do this?
For disabling the button: [self.howButton setEnabled:NO];
But where do I put this?
you can add your keyboards stament to the notifacticon , when it show , then disable the button , when it hide , then enable the button
in the init to add the observer
- (void)init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
}
achieve the show and hide method
- (void)keyboardWillHide
{
[self.howButton setEnabled:YES];
}
- (void)keyboardWillShow
{
[self.howButton setEnabled:NO];
}
finally remove the observer , when the class is dealloc
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Register for UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
In the keyboardWillShow method disable your button.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
You can re-enable the button if required by registering for the above notification.