iOS Keyboard notifications triggered unnecessarily upon showing the alertviews on iOS8.3 - ios

We are observing unusual behaviour with respect to Keyboard willshow & will hide notification on iOS 8.3.
The viewcontroler (listenig to keyboard notifications) has a textfiled and upon clicking and upon tapping the submit button, the method first resigns the first responder from textfield, and shows an alert to inform warning. Everything works fine, it dismisses the keyboard and shows up the alert as expected. (calls the UIKeyboardWillHideNotification method too).
However, on 8.3, after tapping OK/Cancel on Alertview delegate, it dismisses the alert and it calls up UIKeyboardWillShowNotification & UIKeyboardWillHideNotification respectively, though it was not supposed to be called! This was not expected, as the keyboard was already dismissed before dispalying the alert!
Here is the code snippet, that we are trying:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (IBAction)ShowAlert:(id)sender {
[self.TxtField resignFirstResponder];
//This woudln't make any diff either :(
[self.view endEditing:YES];
[self ShowAlertForTest];
}
-(void)ShowAlertForTest{
UIAlertView *theAlertView= [[UIAlertView alloc]initWithTitle:#"Title"
message:#"msg"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[theAlertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"buttonIndex = %ld",buttonIndex);
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSLog(#"keyboardWillShow");
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSLog(#"keyboardWillHide");
}
This behaviour is causing issues in our app, when there are cascading alerts triggered from the previous alertview'd delegate - bringing up the keyboard in unneeded situations.
Any help /advice is greatly appreciated!

In our case the keyboard was hidden manually by the app (e.g. when user taps Log In, we hide keyboard and call the server login API). Upon failure the app presents UIAlertView with an error message. When user closes the alert, iOS posts will/did hide & will/did show notifications. Of course the keyboard is not shown & hidden during this sequence, because it is already hidden by the app.
However, we noticed that not hiding keyboard manually, but instead letting iOS to do it for us, fixes the issue. So, the keyboard is hidden automatically in two cases:
when UIAlertView is shown
when view controller is deallocated
Note: the keyboard is automatically shown when UIAlertView is dismissed.

My team did a work around by merely unsubscribing from the keyboard notifications before presenting the alert view and resubscribed to those notifications after the alert view had been dismissed. Not ideal but it resolved the issue for us.

In my case user clicks the login button, then I call;
[self.view endEditing: YES];
//server request here and in completion/fail alert.
Keyboard was closed, alertview is shown nicely, but on cancel/apply click keyboard was shown again and disappeared.But the problem was this was happening sometimes, if server request takes time problem is not seen, if Alertview is shown immediately problem still there. So I decided to call my alerts with delay. Putting delay on alert is solved my problem. Hope this helps.

I just fixed a similar issue. The keyboard keep popup after the alert dismisses It seems like a bug of apple.
I recommand you to use UIAlertController instead of UIAlertView. It will avoid a lot of potential issues
There is a simple solution: If you are using UIAlertController, you can just set the animated to NO
[self presentViewController:alert animated:NO completion:nil];
Let me know if it fixed your problem

Related

Dismiss keyboard from inside objective C resignActive (UIApplicationWillResignActiveNotification) double tapping on ipad home button

I have tried to dismiss keyboard when user double press on home button, but failed to dismiss.
Basically I want to show splash screen in recent app tray when user double tap on home button. When there is no keyboard available the splash screen is showing correctly but when keyboard is there, view is not changed and not showing the splash screen.
Below code I have added its executing but not working for me.
In applicationWillResign
[searchBar resignFirstResponder];
[searchBar endEditing:true];
in viewDidLoad
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];

How to dismiss multiple view controllers which have been presented not pushed?

Scenario:
I need to show 3 or more popups one after the other on button click in each popup. I have created a different viewcontroller and xib files for each popup. So for displaying each popup I have used presentViewController instead of pushViewController.
That is, I have used this:
[self presentPopupViewController:searchPopUpView animationType:0];
instead of
[self.navigationController pushViewController:searchPopUpView animated:YES];
For dismissing a popup, the following code has been written:
[self dismissPopupViewControllerWithanimationType:0];
Issue:
The popups are displaying perfectly, but the background gets darker and darker whenever a popup shows up. After all popups have been dismissed I have to finally click on the blank screen to remove those darker parts. How to overcome this issue?
I think you are using MJPopupViewController to show pop-up.
If it is so, Then try this.
Suppose there is a controllerA from which you want to show a pop-up controller popupControllerB.
Then in your controllerA add Notifications Observer
Code to write in controllerA :
// Add Notification Observer when your view initialise.
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(dismissPopup) name:#"DISMISS_POPUP" object:nil];
In viewWillDisappear remove the notifications observer
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
This method will be called when you Post-notification from your popupControllerB
-(void)dismissPopup {
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
And In popupControllerB, Where you want to dismiss the Pop-up, write this code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"DISMISS_POPUP" object:nil];
Above line of code will call a method written in your controllerA and dismiss the pop-up properly.
If you want to dismiss presented UIViewControllers you can use this code. I have used this approach to dismiss presentedViewControllers. It will dismiss all your presentedViewControllers on your rootViewController.
UIViewController* presVC = self.window.rootViewController;
while (presVC) {
UIViewController* temp = vc.presentingViewController;
if (!temp.presentedViewController) {
[vc dismissViewControllerAnimated:NO completion:^{}];
break;
}
vc = temp;
}

Dealing with the same NSNotification in multiple view controllers

I'm using the below notifications to reload ViewControllerA when my app comes back from background mode. It works correctly, but the applicationEnteredForeground: method gets called every time when I open the app again. For example if I close the app when ViewControllerB or ViewControllerC is on the screen and open it again the method will be called despite the viewDidLoad of ViewControllerB doesn't contain applicationEnteredForeground: method. I would like to know that how could solve this issue? My goal is to use applicationEnteredForeground: only when ViewControllerA was on the screen before I closed the app.
As a possible solution I would just remove the NSNotificationCenter in the viewDidDisappear, but since the observer is in the viewDidLoad it won't work when the user navigates back, because viewDidLoad won't be called again. Is there any fix for this?
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationEnteredForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)applicationEnteredForeground:(NSNotification *)notification {
// do stuff...
}
You should remove ViewController A's event listener on viewWillDisappear and add it in viewWillAppear. That way, VC A will only be listening when it is the visible view controller.
You can check if a view controller is on screen by checking the window property of it's view. It will work in most standard cases.
- (void)applicationEnteredForeground:(NSNotification *)notification
{
if (self.view.window == nil) {
// Not on screen
return;
}
// do stuff...
}

Pressing on "Done" button while playing a YouTube video dismisses the rootViewController

I have UINavigationController which needs to be displayed right after my root UINavigationController using storyBoard segue.
[self performSegueWithIdentifier:#"LoginViewController" sender:self];
This storyBoard segue is Modal.
On top of this LoginViewController I embedded YouTube video using the snippet here.
While the video is playing, pressing on the "Done" button will cause my LoginViewController to be dismissed along with the video.
It also happens when the video ends.
Is there any way to handle the "Done" button? is there any notification which Let me know when the video ends?
Thanks in advance
Add the UITextfield Delegate to your class, then set the delegate property of your uitextfield to "self".
Handle the "done" (aka "return") key with the following method:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[textField resignFirstResponder];
return NO;
}
I found a notification which helped me to know when the video end but you will have to use it carefully.
Apparently, When the youTube video plays, it appears on top of the UIWindow, So just before you trigger your video register the UIWindowDidBecomeVisibleNotification notification. Like this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowNowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window
];
When the video ends, The UIWindow comes visible again and and there you should remove the notification.

UIPopoverController not moving to original position after keyboard slides back down

I'm displaying a popover in iPad with a UINavigation bar. On the second view, I have a UISearchController that can display a keyboard. The keyboard pushes the popover up, which is fine, however if I now push the 'back' button on the UINavigation bar it dismisses the keyboard which is fine, but the popover doesn't slide back down to its original position. Anyone know how to fix that? Thanks!
Ok so I actually figured out (I believe) what your question was asking...and just in case anyone stumbles upon this from google, I figured I'd answer how I did it. It feels like a hack job but I haven't been able to find any other way to do it.
In the controller that brings up the keyboard,I had it post a notification whenever the keyboard dismisses:
[aTextField resignFirstResponder];
[[NSNotificationCenter defaultCenter] postNotificationName:#"movePopups" object:nil];
Then back on my home screen controller, that controls the UIPopover, I added a listener:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movePopUpToRightLocation)
name:#"movePopups"
object:nil];
inside the init. Be sure to remember to remove the listener in your dealloc for good programming practice:
[[NSNotificationCenter defaultCenter] removeObserver:self];
So then whenever I get notification that the keyboard disappears, I get a reference to the button that the popover shows up from, and just have it re-appear directly from it:
-(void)movePopUpToRightLocation {
NSLog(#"move pop up to right location");
if (morePopUp) {
UIBarButtonItem *barButtonItem = (UIBarButtonItem *)[[bottomToolBar items] objectAtIndex:0];
[morePopUp presentPopoverFromBarButtonItem:barButtonItem
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
}
I haven't added any checks for which popup it is, but I can easily do that if I have more than 1 type of popover / button that it would appear from. But that's the basic premise that you can go from.
Hope it helps!
You could also register for the UIKeyboardDidHideNotification somewhere in the initializer.
[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movePopoverBack:) name:UIKeyboardDidHideNotification object:nil];
This code moves the popover back:
- (void)movePopoverBack:(id)sender {
if ([self.settingsPopoverController isPopoverVisible]) {
[self performSelector:#selector(hidePopover) withObject:nil afterDelay:0.1];
[self performSelector:#selector(movePopoverBack) withObject:nil afterDelay:0.5];
}
}
- (void)hidePopover {
[self.settingsPopoverController dismissPopoverAnimated:YES];
}
- (void)movePopoverBack {
[self.settingsPopoverController
presentPopoverFromBarButtonItem:self.bottomToolbar.settingsButton
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
I didn't get it working without the delays, but this seems to be acceptable for my current project. Hope it helps someone.
After you press the Back button you should manually call resignFirstResponder for the search field (for example inside viewDidDisappear).
This should help, but the issue still will be reproduced under iOS 4 when the device is in Landscape orientation with Hthe ome button on the left side

Resources