Keyboard will appeared automatically in ios 8.3 while displaying alertview or alertcontroller - uialertview

I have updated Xcode 6.3 and ios8.3 check my code. then it gives me weird result.
here is first screen of my demo app. here is one textfield. when I type somethin in textfield keyboard open.
after typing completed. I have clicked on show alert button. I have displayed alert and output will be following.
After click on cancel.
I have displayed another alert then weird result keyboard should not open but when click on cancel button. display another alert and keyboard will appear automatically.
here is next screen output
following is the code
- (IBAction)MethodShowAlert:(id)sender
{
[tmptxtField resignFirstResponder];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Check Alert textField" message:#"keyboard should not be open" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self showCustomAlertWithTitle:nil];
}
-(void)showCustomAlertWithTitle:(NSString *)title{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Now Check" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alertView show]
}

In my case i tried hiding keyboard, before showing alert, so it will not save keyboard in memory to present it again after dismissing it self.
for that you just need to dismiss keyboard which will take default animation time to hide, only then you should present alert view then it will not save that keyboard.
you must put around .6 second gap in hiding keyboard and presenting alert
[YOUR_TEXT resignFirstResponder];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_alertVw = [[UIAlertView alloc] initWithTitle:#"" message:#"message." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[_alertVw show];
});

Yep, it's strange.
But since iOS 8, I suggest to use the UIAlertController instead of UIAlertView.
Replace your code with this one:
- (IBAction)MethodShowAlert:(id)sender
{
[tmptxtField resignFirstResponder];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:#"Check Alert textField"
message:#"keyboard should not be open"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self showCustomAlertWithTitle:#"Now Check"];
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)showCustomAlertWithTitle:(NSString *)title{
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];
}
The keyboard will not show after the click on the button.

This was a change in behaviour introduced in iOS 8.3. Try downloading the iOS 8.2 simulator and you will see the old behaviour.
The result of my analysis was the following:
When an alert is shown, it saves the currently showing keyboard.
When an alert has completed the dismiss animation, it restores the previously saved keyboard.
So in -[id<UIAlertViewDelegate> alertView:clickedButtonAtIndex:], you are between those states. So what happens with two Alerts that are shown at the same time:
Show Alert1. Save visible keyboard. Hide keyboard.
User taps on alert.
Show Alert2. Save that there is no keyboard.
Alert1 completes dismiss animation. Restore saved keyboard. Keyboard is visible.
User taps on alert.
Alert2 is dismissed. Restore that there is no keyboard. Hide keyboard.
My recommendation is to use a UIAlertViewDelegate method that is called after the dismiss animation completes and show the next alert then.

Replace your alert method from below method.
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:messege title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
}];
[alertVC addAction:cancelAction];
[[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
}];

I also tried hiding the keyboard directly before presenting the alert.
What worked for me was calling [myTextField endEditing:YES]; instead of [myTextField resignFirstReponder];
This let the keyboard stay hidden, even after the alert was dismissed again.
The code looks like this:
[myTextField endEditing:YES];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"myTitle"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"NO"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"YES"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

Related

Application crash due to UIAlertController in ios 8

I am developing custom keyboard app and in this app want to show UIAlertController but it crash with following error.
Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
I used following code...
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
So how can i solve?

UIAlertView changes to landscape when App is locked in portrait only in iOS8

I have an app and its orientation is locked as portrait mode, but when I have a UIAlertView and turn the iPhone to landscape, the alert changes the orientation and crops the alert's overlay. Only in iOS8.
Anyone had this error?
Yes I did get this issue. I have developed my code in iOS7 but had to do compatible with iOS 8 also. So I came across This link. I got my solution as follows:
When you are working with iOS7 UIAlertView works fine but if you are working with iOS8 you have to use UIAlertController. Use this kind of code:
if(SYSTEM_VERSION_LESS_THAN(#"8.0"))
{
alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Invalid Coupon." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Warning" message:#"Invalid Coupon." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Hope this will help you.

Detect if back button is pressed AND wait user action to pop to previous view

I saw multiple questions about how to detect when the user is pressing the "Back" button on a UINavigationBar, but the answers does not solved my problem.
Indeed, I want to display a UIAlertView when user is pressing the UINavigationBar "Back" button, to ask him "Do you want to save the changes ?".
I can display a UIAlertView when the user is pressing the "Back" button (with the following snippet), but the previous view is popped in the same time. And I don't want this behaviour ! I just want that the app WAIT the user answer BEFORE pop the previous view...
-(void) viewWillDisappear:(BOOL)animated
{
if ([self isMovingFromParentViewController])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Avertissement" message:#"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:#"Retour" otherButtonTitles:#"Oui", #"Non", nil];
[alert show];
}
[super viewWillDisappear:animated];
}
Thanks for you help...
I Suggest you using you own LeftbarButtonItem instead of default Back-button event at viewWillDisappear like this:-
- (void)viewDidLoad
{
UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(actionBack:)];
self.navigationItem.leftBarButtonItem = left;
[super viewDidLoad];
}
- (IBAction)actionBack:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Avertissement" message:#"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:#"Retour" otherButtonTitles:#"Oui", #"Non", nil];
[alert show];
}
And use alert Delegate clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
// set your logic
}
}

Don't Pop Navigation Controller in Xcode if AlertView

I have a Navigation Controller and when I press the back button I don't want to pop the view but I want to show an UIAlertView.I just want to pop the view after having make a choice on the UIAlertView!
How can I do?
I tried to catch the event 'back button pressed' but with no results :(
Create a UIBarButtonItem inside viewDidload and added it to the navigation bar
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
create a method which will be called when the backButton is clicked
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Pop View!" message:#"Are you sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NOTE: Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>
You can check for back-button press in the viewWillDisappear method like this (got it from here):
-(void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
Hope this helps!
Implement pop in UIAlertview delegate method clickedButtonAtIndex:
The back button press need to implement only showing the UIAlertview
refer this
follow this code
in button action.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Do you want to open?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert show];
//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//NO
{
//your code
}
else if(buttonIndex==1)//YES
{
// your code
}
}

iPhone AlertView Text Field Keyboard not visible

I have an alert view with a uitextfield added as a subview.
In my uitableview controller the keyboard shows fine.
However in a different view I wanted to do the same thing. So instead of using a UITableView Controller I made it a UIViewController so that I could add a toolbar at the bottom of the view.
But when I display the UIAlertView the keyboard is hidden. I'm thinking it's behind the view because the alert view moves up to make room for the keyboard.
Any ideas?
UPDATE:
The reason the keyboard was being hidden was because I was dismissing a modalviewcontroller after showing the alert. For some reason it would dismiss the keyboard also I guess. Just rearranged the order an fit works fine now...
Try implementing the didPresentAlertView: method and inside set the text field to firstResponder like so:
- (IBAction)someActionThatTriggersAnAlertView:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *someTextField = [alert textFieldAtIndex:0];
someTextField.keyboardType = UIKeyboardTypeAlphabet;
someTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
someTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
#pragma mark - UIAlertViewDelegate Methods
- (void)didPresentAlertView:(UIAlertView *)alertView {
[[alertView textFieldAtIndex:0] becomeFirstResponder];
}
UIAlertView Documentation
UIAlertViewDelegate Documentation