I have a login screen Where there are two text fields for username and password. When email is incorrect then i moved down the keyboard and show the appropriate alert for the user. When i clicked on cancel button of alertview then alert view disappears and scroll view goes upwards. This issue only comes on devices after iOS 8.1. On iOS 8.1 and earlier, it works fine. I don't understand what is the exact cause? Below is the code
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (txtActiveField == txtUsername) {
[txtPassword becomeFirstResponder];
}
else if (txtActiveField == txtPassword) {
[txtPassword resignFirstResponder];
//[self loginViaEmail:self];
[self performSelector:#selector(loginViaEmail:) withObject:btnLogin afterDelay:0.0];
}
return YES;}
-(IBAction)loginViaEmail:(id)sender{
//[txtActiveField resignFirstResponder];
if ([[txtUsername.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]] length] == 0)
{
[AppDelegate showWithTitle:title message:#"Please enter Username/Email for your account"];
}
else if ([txtPassword.text length] < 6)
{
if([[txtPassword.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]] length] == 0)
[AppDelegate showWithTitle:title message:#"Please enter Password"];
else
[AppDelegate showWithTitle:title message:#"Password contain minimum 6 characters"];
}
#pragma mark Show alert with message
+(void)showWithTitle:(NSString *)title message:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
I think it is the issue with Alert view. As from iOS 8, AlertViewController comes instead of alert view. Try using below code which works fine on iOS 8.4 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg 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:^{
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
Any one has any idea how to fix this for iOS 8 device. is it Apple's implementation issue for UIAlertview ?
I am having screen shot for the same. But can not upload due to reputation limit :(
In IOS8 added UIAlertController instead of UIAlertView
if (([[[UIDevice currentDevice] systemVersion] compare:#"8.0" options:NSNumericSearch] == NSOrderedAscending)) {
// use UIAlertView
}
else {
// use UIAlertController
[self showAlert];
}
-(void)showAlert
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"AlertView" message:nil preferredStyle:UIAlertControllerStyleActionSheet;
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"GO" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// [action doSomething];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:defaultAction];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
Try with that solution.
why UIAlertController in iOS7 received nil value while need to presented but worked in iOS8 was good, may i know that is because iOS7 not support the UIAlertController class ?
UIAlertController *view=[UIAlertController
alertControllerWithTitle:#"Hello"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:view animated:NO completion:nil];
These class is only available in iOS8 and later. See the class reference
In order to display AlertView in both iOS 8 and lower versions you can use the following code:
if ([self isiOS8OrAbove]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
} else {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertView show];
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)isiOS8OrAbove {
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: #"8.0"
options: NSNumericSearch];
return (order == NSOrderedSame || order == NSOrderedDescending);
}
You might be using os version less then iOS 8.
If you compile your code with Xcode 6 and use a device with iOS version < 8.0 then you will face this issue. I would suggest the following
- (void)showXYZAlert
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0"))
{
// Handle UIAlertController
[self showXYZAlertController];
}
else
{
// Handle UIAlertView
[self showXYZAlertControllerView];
}
}
I have an UIAlertView which is getting shown perfectly in iOS 7 but in iOS 8, it does not show any buttons or labels. Alert is still visible but just a small white box.
The OK and cancel buttons take their events as well but no texts are visible.
I have used this alert to show on click of a button
- (IBAction)sel_btnLogout:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Logout!" message:#"Are you sure you want to logout?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
}
I checked the frame in iOS 8: it is giving (0,0,0,0) but in iOS 7 it is giving a definite value.
I also checked for iterating into the subviews of uialertview. In iOS7, it goes in the loop, as it finds alert's subviews. In iOS8, it says there are no subviews of alertView.
Check if the class is available
if ([UIAlertController class])
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert title" message:#"Alert message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
else
{
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"Alert title" message:#"Alert message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
With iOS 8 you can set the title instead of the message:
[[[UIAlertView alloc] initWithTitle:#"AlertView in iOS 8." message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
UIAlertView is deprecated from iOS 8 for more information visit this
http://nshipster.com/uialertcontroller/.
https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/index.html
So if you're going to write separate code for iOS 7 and iOS 8, you should be using UIAlertController instead:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"AlertView in iOS 8" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alertController animated:YES completion:nil];
I got the answer to my issue. The issue was that I was using UIFont+Replacement category in my project. This was working fine on iOS 7 but on iOS 8 it was using few deprecated methods. Due to this, I don't know why, but only my alert view was not showing any labels.
Solution: Deleted the category from the project and set font through xib. Once we place the .tff file of any font in our project workspace, we see those font names in the xib under custom fonts. NO NEED TO USE UIFont+Replacement category.
Please read through the code below to understand how to add the fields and buttons to the alerts.
- (IBAction)UIAlertControllerWithActionSheetTextFields:(id)sender {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController with Actionsheet and text fields"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Resolving UIAlert Action for tapping OK Button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Resolving UIAlertActionController for tapping cancel button");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.accessibilityIdentifier = #"usernameTextField";
textField.placeholder = #"Enter your username";
textField.accessibilityLabel = #"usernameLabel";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * textField) {
textField.placeholder = #"Enter your password";
textField.accessibilityIdentifier = #"paswordTextField";
textField.accessibilityLabel = #"passwordLabel";
}];
[self presentViewController:alert animated:YES completion:nil];
}
and if you need a project to completely refer the types of Alerts that are available in IOS, please follow my project from the below URL:
Alert variations in IOS
in iOS 8 you need to replace UIAletrview and UIActionSheet with UIAlertcontroller . You read first This documentation in apple forum
Apple Alertcontroller
You can check that code
if (([[[UIDevice currentDevice] systemVersion] compare:#"8.0" options:NSNumericSearch] == NSOrderedAscending))
{
// use UIAlertView
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:Title message:desc delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
else {
// use UIAlertController
UIAlertController *alert = [UIAlertController alertControllerWithTitle:Title message:desc preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
}
let alert = UIAlertController(title: "Warning" as String , message: messageString as String, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.dismissViewControllerAnimated(true, completion: nil)
}
// Add the actions
alert.addAction(okAction)
self.presentViewController(alert, animated: true, completion: nil)
i think this is not UIAlertview Issues.
please check this work fine..
i think your code issues...........
in any view controller put this code in viewDidLoad Like below:
- (void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"My message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
}
I have a old Xcode project that is targeted for iOS 6 and higher. I recently opened it up in Xcode 6 and ran in iPhone 6 simulator for iOS 8. When I tried this action
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
I get a pop-up window but when I click on the text field, no keyboard comes up. I googled and read that I need to use UIAlertController instead. Since I need to support iOS 6, 7 versions as well so I changed my code like this.
if ([UIAlertController class])
{
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(#"text was %#", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"folder name";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
// use UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Folder Name"
message:#"Keep it short and sweet"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
dialog.tag = 400;
[dialog show];
}
Again if I try the same action NO keyboard is displayed.
Is this a bug in Xcode 6 simulator or I am doing something wrong?
I Tested this, if iOS8 detects a hardware keyboard , than it does not open the keypad. As you might be testing in simulator , so it is not Showing any keypad
Press "Command" + "k" and you should be able to see the keypad.
When testing on a device you will not face this issue , unless user has connected his device with a hardware Bluetooth keypad , so this is not something you should worry about
Hope this helps
Another way of seeing the keyboard is setting a keyboard type. This could be an example code:
UIAlertController* alertController = [UIAlertController
alertControllerWithTitle:#"Test"
message:#"Testing keyboard"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler: ^(UITextField *tf)
{
tf.autocorrectionType = UITextAutocorrectionTypeYes;
tf.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alertController animated:YES completion:nil];
Then you will see simulator's keyboard when you show the AlertController