Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alertViewShouldEnableFirstOtherButton not called

I want to show UIAlertview With textfield. And if User has not entered valid emai id then ok button of UIAlertview will be disable.

I know How to show UIAlertview and disable UIAlertview Ok button ..

My problem is -(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView not called thats why button is enabled.

But When user press Ok button then

- (void)alertView:(UIAlertView
*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

is called.

Here is my code ,

-(IBAction)forgotpassBtnclicked:(id)sender
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertview setAlertViewStyle:UIAlertViewStylePlainTextInput];
UITextField *textField=[alertview textFieldAtIndex:0];
textField.placeholder=@"enter your email id";
[alertview show];
} 


#pragma  mark Alertview delgate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{

UITextField *text1=[alertView textFieldAtIndex:0];
BOOL flag=[self validateEmail:text1.text];
return flag;

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

}

#pragma mark Email Validation
- (BOOL) validateEmail: (NSString *) email 
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
//  return 0;
return [emailTest evaluateWithObject:email];
}

i don't understand what am i doing wrong?..

Thanks in Advance...

like image 237
Kalpesh Avatar asked Jul 07 '26 01:07

Kalpesh


1 Answers

UIAlertViewDelegate documentation says for alertViewShouldEnableFirstOtherButton: method,

Sent to the delegate to determine whether the first non-cancel button ion the alert should be enabled.

You are creating the alert with cancel button as @"ok", since non-cancel buttons is nil the method is not getting called. Instead try following,

UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

Also, user should have option to cancel the alert without having to enter text. It is good user experience to have a cancel button.

like image 182
Amar Avatar answered Jul 09 '26 16:07

Amar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!