What is correct way to turn off dismiss UIAlertView when click button in UIAlertView. I want to check if not have string in UITextField when click button not dismiss the view.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//Add alert delegate
if (alertView.tag == kDeleteAlertTag)
{
if (buttonIndex == 0)
{
[self.emails removeObjectAtIndex:currentIndex];
[self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
[self.tblEmails reloadData];
}
}
// remove email alert delegate
if (alertView.tag == kAddEmailTag)
{
if (buttonIndex == 0)
{
// get text Input Alert
UITextField * emailInputTextField = [alertView textFieldAtIndex:0];
// get string from text Alert
NSString *textInput = [emailInputTextField text];
if (self.emails== nil) {
self.emails = [[NSMutableArray alloc]init];
}
if ([textInput isEqualToString:#""]) {
NSLog(#"Not have string");
}else
if ([KUtils NSStringIsValidEmail:textInput] == YES)
{
[self.emails addObject:textInput];
// save to plist and reload table
[self.plistReader saveToPlistName:kPlistEmails fromArray:self.emails];
[self.tblEmails reloadData];
}else if([KUtils NSStringIsValidEmail:textInput] == NO)
{
// not dismiss alert in hre
}
}
}
Don't validate against the string validate against the text field
if (emailInputTextField.text && emailInputTextField.text.length > 0)
{
//do something
}
else
{
//All fields are required UIAlert here
}
Edit
You can also try:
if ([emailInputTextField.text length] != 0) {
//Do something
}
else {
//All fields are required UIAlert here
}
Related
I'm attempting to create a simple webpage browser-esque application, where whenever the user wants to use a back, home, or forward function they are asked through an alert view to confirm.
The console window confirms that it has no problem calling the IBAction but comes up blank whenever I expect it to call Alert View.
Any help would be appreciated.
- (IBAction)controlPanelActions:(id)sender{
if (_controlPanel.selectedSegmentIndex == 0)
{
if ([_viewWeb canGoBack])
{
[self askToGoHome:nil];
}
}
- (IBAction)askToGoHome:(id)sender {
UIAlertView *alertDialog;
alertDialog.tag = 2;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Confirm"
message:#"Continue with Home Action?"
delegate: self
cancelButtonTitle: #"Yes"
otherButtonTitles: #"No", nil];
[alertDialog show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 1)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"back - yes");
[_viewWeb goBack];
}
}
if (alertView.tag == 2)
{
NSLog(#"home - pre-yes");
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"home - yes");
HOMEPAGEURL = [[NSURL alloc] initWithString:HOMEPAGE];
[self.viewWeb loadRequest:[NSURLRequest requestWithURL:HOMEPAGEURL]];
}
}
if (alertView.tag == 3)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
[_viewWeb goForward];
}
}
}
You set alertDialog.tag = 2;before you call init.
So,everytime you set tag,you set tag to a nil.It will not work.
Alertview alloc and init method sets its tag to 0.
So alertDialog.tag = 2;
will not work.
add this line after alloc and init method.
I am trying to see what option the client has pushed on the alert view button, though the logging doesn't appear to be working
-(IBAction)flattenBtn:(id)sender
{
UIAlertView* flatView = [[UIAlertView alloc] initWithTitle:#"Flatten Image"
message:#"Are you sure you want to flatten your image?" delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Yes",#"No",nil];
[flatView show];
[flatView release];
}
- (void)flatView:(UIAlertView *)flatView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"YES");
}
else if (buttonIndex == 1)
{
NSLog(#"NO");
}
}
Try it using like this :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Yes"])
{
//your code if YES pressed;
}
else
{
//your code if NO pressed;
}
}
I have created textfields dynamically in UITableView I don't want to pass cursor without entering value in first textfield
[self.playerTable.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL*stop)
{
UITableViewCell *cell = obj;
if([cell isKindOfClass:[UITableViewCell class]])
{
for(UITextField *textField in cell.contentView.subviews)
{
if([textField isKindOfClass:[UITextField class]])
{
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
isEditMode = NO;
if(!isEditMode && [playerstr length] > 0)
{
NSMutableArray *playerinfoArry = [dbWrapper getPlayerInfo];
for (Playerinfo *player in playerinfoArry)
{
if ([player.playername isEqualToString:playerstr])
{
isPlayerExist = YES;
isEditMode = !isEditMode;
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[_nameField resignFirstResponder];
[alert showInView:self.view];
NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:selectedRow inSection:0];
[_playerTable selectRowAtIndexPath:indexPath1 animated:YES scrollPosition:UITableViewScrollPositionTop];
return;
}
}
}
}
}
}
}
}];
instead of looking for the first responder while trying to edit another UITextField, have you tried the other approach: not permitting the UITextField to resign as first responder. This could be something like:
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField{
for(UITextField *otherTextField in self.view)
{
if ([otherTextField isKindOfClass:[UITextField class]] && [textField.text isEqualToString:otherTextField.text]){
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[alert showInView:self.view];
return NO;
}
}
return YES;
}
I hope this helps.
Im trying to do an app with 4 ImageViews and 4 UIButtons. When one of the UIButtons are pressed a UIAlertView should appear with 3 options. One named "Picture" should open the photo library so the user can change the ImageView picture. I have wrote a code for this but it wont work. Do anyone have any suggestions on how to make it work?
Thanks in advance!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *buttonOne = changeButtonOne; //the UIButtons
NSString *buttonTwo = changeButtonTwo;
NSString *buttonThree = changeButtonThree;
NSString *buttonFour = changeButtonFour;
if([buttonOne isEqualToString:#"buttonOne"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController = [[UIImagePickerController alloc]init]; //I think its this part which are wrong
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
}
else if([buttonTwo isEqualToString:#"buttonTwo"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"Yoy pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController2 = [[UIImagePickerController alloc]init]; //I think its the part thats wrong.
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];
}
}
else if([buttonThree isEqualToString:#"buttonThree"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController3 = [[UIImagePickerController alloc]init]; //i think this is the wrong part
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];
}
}
else if([buttonFour isEqualToString:#"buttonFour"])
{
if([title isEqualToString:#"Done"])
{
NSLog(#"You pressed done");
}
else if([title isEqualToString:#"Phone number"])
{
NSLog(#"You pressed Phone number");
}
else if([title isEqualToString:#"Picture"])
{
imagePickerController4 = [[UIImagePickerController alloc]init]; //this is probably the wrong part
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];
}
}
}
#end
You should use the tag property of UIAlertView to differentiate between the alerts and the buttonIndex (not the button title) to know which button was pressed.
So don't use isEqualToString at all in the alert view delegate method. You want this to work with different languages.
In the button action, before you show the alert:
alertView.tag = 1; // 1 = button1, 2 = button2 etc.
Alert view delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { //button1
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 1
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 2
}
}
if (alertView.tag == 2) { //button2
if (buttonIndex == alertView.cancelButtonIndex) {
// handle cancel button
}
if (buttonIndex == alertView.firstOtherButtonIndex) {
// handle action 3
}
if (buttonIndex == alertView.firstOtherButtonIndex+1) {
// handle action 4
}
}
}
I want to show a login dialogue and login error dialogue if necessary.
I use UIAlertView to show these dialogues, but the process keep running while showing the UIAlertView.
I wrote a code below. Now NSUserDefaults doesn't keep those value, so I expected login dialogue is shown and wait until button to be tapped.
But when run this, error dialogue is shown and after tapping OK for this, login dialogue is shown.
How can I fix this?
Thanks in advance.
- (void)storeEvernote
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
evID = [defaults stringForKey:#"evID"];
evPW = [defaults stringForKey:#"evPW"];
NSLog(#"%#", evID);
if( evID == NULL || evPW == NULL )
{
login = true;
[self showLoginDialogue];
}
else
{
evernoteID = evID;
evernotePW = evPW;
}
if( evernoteID == NULL || evernotePW == NULL )
{
login = false;
[self showErrMessage];
return;
}
[self getEvernoteNotebooks];
}
- (void)showLoginDialogue
{
UIAlertView *loginDialogue = [[UIAlertView alloc] initWithTitle:#"Evernote Login" message:#"Enter your Evernote Info" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Login", nil];
loginDialogue.delegate = self;
loginDialogue.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[loginDialogue show];
}
- (void)showErrMessage
{
UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:#"Login Failure" message:#"Invalid ID & Password" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[loginalert show];
}
The first time that storeEvernote method runs, evID and evPW are NULL so the first if is true, it shows the LoginDialoge then it continues to second if and because evernoteID and evernotePW are still NULL so the condition in second if statement is also true, so it shows the errorMessage. To fix this, move the second if statement to delegate method of the loginDialogue ALSO ADD return; to the end of first if statement for example:
- (void)storeEvernote
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
evID = [defaults stringForKey:#"evID"];
evPW = [defaults stringForKey:#"evPW"];
NSLog(#"%#", evID);
if( evID == NULL || evPW == NULL )
{
login = true;
[self showLoginDialogue];
return;
}
else
{
evernoteID = evID;
evernotePW = evPW;
}
[self getEvernoteNotebooks];
}
//The login dialoge delegate method:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Process user input
...
if( evernoteID == NULL || evernotePW == NULL )
{
login = false;
[self showErrMessage];
return;
}
[self getEvernoteNotebooks];
}