alertViewShouldEnableFirstOtherButton not called - ios

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...

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.

Related

Change iOS tab view from alert

I have a tabbed application with 2 tab views. I want to change the view from the second tab to the first when the user presses "OK" on an alert view.
The following code snippet works when I use it on an -IBAction button pressed, but it doesn't do anything when I use it inside the alert code:
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Why does it not work when used as below, in my SeondViewController.m in my
- (void)viewDidLoad {
[super viewDidLoad];?
UIAlertView *alert =[[UIAlertView alloc]
initWithTitle:#"Camera and Target coincident"
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
I'm just learning this, so anything you can offer would be helpful.
When I use the following code:
if ([ theProjection Trans_Initialise] == 1) {
NSString * msg = nil;
UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}
I get the error message on -(void) alertView "Invalid argument type void to unary expression" Am I doing something grammatical, or is it (quite possibly) something I just didn't understand?
First of all, you need a delegate call back from the alert view informing you about the button that was clicked.
Your alert view should be created like this :
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Camera and Target coincident" message:msg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
Then in your m file implement the function :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"OK"]) {
self.tabBarController.selectedIndex = 0;
}
}

how to get UIAlertView otherButtons' title?

-(void) alertView: (UIAlertView *) alertVw clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *str = [NSString stringWithFormat:#"%d 。", buttonIndex];
UIAlertView *newAlertVw = [[UIAlertView alloc] initWithTitle:#"INFO" message:str delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[newAlertVw show];
}
UIAlertView *alertVw = [[UIAlertView alloc] initWithTitle:#"TITLE"
message:#"box message"
delegate:self
cancelButtonTitle:#"hide"
otherButtonTitles:#"T1", #"T2", nil];
[alertVw show];
if user touched the one of otherButtons, yes, we know which button that user touched.
then, how can I to get the title of button that user just touched?
thanks.
With the delegate on your UIAlertView set to self you can use this:
-(void)alertView:(UIAlertView*)alertView didDismissWithButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Button Title -----> %#",[alertView buttonTitleAtIndex:buttonIndex]);
// or you can check if it equals to string
if([[alertView buttonTitleAtIndex:buttonIndex]isEqual:#"Enter"])
{
// your code goes here
}
}
The method is buttonTitleAtIndex: and the info is simply found on the UIAlertView doc page.
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/buttonTitleAtIndex:
I advise you to refer to the doc as often as you can this is how you will learn and remember.
Actually, if you want to use the delegate method you have to use -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex.
and not
-(void)alertView:(UIAlertView*)alertView didDismissWithButton At Index:(NSInteger)buttonIndex
see: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intfm/UIAlertViewDelegate/alertView:didDismissWithButtonIndex:
I guess it was just a typo of Pancho.

UIAlertView not responding to button event

First time working with UIAlertViews and I can't figure out why this doesn't work. When I click on either button, the alert closes, but there is no NSLog output. What am I doing wrong? I can't figure it out. Thanks!
.h
#interface LoginViewController : UIViewController <UIAlertViewDelegate>
#end
.m
- (IBAction)resetPassword:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) NSLog(#"button 0");
if (buttonIndex == 1) NSLog(#"button 1");
}
It appears that you never set the delegate on the UIAlertView. It should be:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
This way the UIAlertView is able to call your method, - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, when the user taps a button.
You're setting the delegate to nil. You need to set it to your controller instance, like this:
- (IBAction)resetPassword:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Password Reset"
message:#"A temporary password will be sent to you at the address we have on file."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Reset Password", nil];
[alert show];
}
Implementing the -alertView:clickedButtonAtIndex: delegate method does little good if you never give the UIAlertView an object to serve as the delegate.

Alertview with integer

Hello to everyone i would like to place my thinking about an alertview. I am thinking to create an Uialertview which will prompt the user to input two integers. Then i would like to retrieve these two integers and put the one on a timer and the second in an sql statement. So if anyone can help on how to implement that i would apreciate it. Thank you all.
Here is my code until now
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0] text]);
[alertView show];
You need to use delegate methods of UIAlterView.
First of all make "self a delegate" instead of nil.
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
Than add this function to your class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
NSLog(#"%#", field.text);
}
To be 100% correct add also in your header class information that you implement this protocol .
// Try this
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertView textFieldAtIndex:0].delegate = self;
[alertView show];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Entered: %#", [[alertView textFieldAtIndex:0]text]);
}

display alertview while deleting data in plist

I want to delete data in UITableView using plist and i want to put alertview when delete button is pressed. I want to mention that i have used edit button to delete the data from tableview. code of edit button is as follows: for your information.
self.navigationItem.leftBarButtonItem=self.editButtonItem;
Plz help me solve this problem.
thanx in advance.
In your function write :
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: #"" message: #"" delegate: self cancelButtonTitle: #"OK" otherButtonTitles: nil];
alertView.tag = 1;
[alertView show];
[alertView release];
On alert "OK" button pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
{
if (alertView.tag == 1)
{
//Do your code
}
}
}
Hope it helps...
on you button action write:-
-(IBAction)btnPressed
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}

Resources