- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated{
if (0 == [alerts cancelButtonIndex]) {
NSLog(#"YES");
} else if (1 == [alerts cancelButtonIndex]) {
NSLog(#"NO");
}
}
I have a UIAlertView, here is the initialization of alerts:
alerts = [[UIAlertView alloc] initWithTitle:#"Trumpets" message:#"Are you sure you would like to use one trumpet, this will give you 10 random units" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alerts show];
The error is Expected ')' before '(' token
I don't understand, the NSLogs were to help remember which option was which.
Thanks in advance
You missed the ending bracket:
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated{
if ([alerts cancelButtonIndex] == 0) {
NSLog(#"YES");
} else if ([alerts cancelButtonIndex] == 1) {
NSLog(#"NO");
}
}
Related
This question already has answers here:
Two Alert Views in One View Controller - buttonIndex Response
(4 answers)
Closed 7 years ago.
EDIT:
Problem Solved ==> Simply giving a tag solved the problem.
I have the following problem:
On a view I have two UIalertviews:
NSString *message = [NSString stringWithFormat:#"Users must enter this code to join the meeting: %#", meetingCode];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Meeting code"
message:message
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"Copy to clipboard", nil];
[alert show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
NSLog(#"Code not copied");
}
else
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = meetingCode;
NSLog(#"Code copied");
}
}
and this one:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
AgendaModel* agenda = _meeting.agenda[indexPath.row] ;
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:agenda.id,#"id",agenda.name,#"name", nil];
NSString *message = [NSString stringWithFormat:#"Are you sure that you want to delete : %#?", agenda.name];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning"
message:message
delegate:self
cancelButtonTitle:#"Close"
otherButtonTitles:#"Delete", nil];
[alert show];
NSString *delete_url = [NSString stringWithFormat:#"RestAgendas/delete.json"];
[_meeting.agenda removeObject:agenda];
[self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[JSONAPI getWithPath:delete_url andParams:dict completion:^(id json, JSONModelError *err) {
NSLog(#"%#", json);
}];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == [alertView cancelButtonIndex])
{
NSLog(#"Agenda Won't Be Deleted");
}
else
{
NSLog(#"Agenda Will Be Deleted");
}
}
Now is the problem that I got the error: Duplicate declaration of method 'alertView:clickedButtonAtIndex'
How can I fix this? I tried some stuff I found here but I still can't make it work. Can someone help me?
Yep, like you said, giving a tag allows you to have multiple UIAlertView in one single view.
Same goes for UIActionSheet or UIAlertController
for future reference, even though this is probably a duplicate, simply create your alert like usual and add
myAlert.tag = 123; //any number of your choice
and in alertView:clickedButtonAtIndex
you can find it using a switch or some if's
if (alertview.tag = 123){
// this is my alert
}else if(alertview.tag = 333){
// this is my other alert
}
For what it's worth, I suggest using else if and not just if to avoid the whole method to be read everytime, and order your if's by decreasing likeliness of being called.
Note you can also give tags to buttons, views, cells, labels, and just pretty much every outlet you can find.
On the Sign Up in my app I want to make sure that the user fill in one field. It should not matter which field for the user though. That's the problem I'm having. The closet I get is to force the user to fill in one specific textfield like the code below. And that's not the best UX i guess :) Anyone who know what to do?
self.saveButton.delegate = self;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([self.RandomTextField.text isEqualToString:#""]) {
self.saveButton.enabled = NO;
} else {
self.saveButton.enabled = YES;
}
return YES;
}
- (IBAction)SignUPBtn:(UIButton *)sender {
if(txtName.text.length==0 && txtEmail.text.length==0 && txtPass.text.length==0)
{
[[[UIAlertView alloc] initWithTitle:#"" message:#"Please Fill Atleast One Field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
}
else
{
//Write Your Code here. this else called only if anyone of three textfield is filled up.
}
}
in the click event of the sign_up button
- (IBAction)SignUPBtn:(UIButton *)sender {
if([textfeld_Email.text length]>0)
{
//sign up
}
else
{
if([textfeld_Pswd.text length]>0)
{
//sing up
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Please enter your email and pass" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
}
I have been successfully created two alerts with four buttons. The first AlertView works great but the second alertView crashes my up. Here is my code:
-(void)showAlertWithTextField{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"info" message:#"Set Time For The Game." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"cancel", nil];
[alertView setTag:101];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
[alertView show];
}
-(void)showexitAlertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:#"Timeout" message:[NSString stringWithFormat:#"Your final score is: %i", runningSore] delegate:self cancelButtonTitle:#"Restart" otherButtonTitles:#"Exit", nil];
[alertView setTag:102];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if (alertView.tag == 101 && buttonIndex == 0) {
//if (buttonIndex == 0){
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1){
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
//controller.countdownLabel.text= score;
[self presentViewController:controller animated:YES completion:nil];
}
else if (alertView.tag==102 && buttonIndex == 0){
NSLog(#"Restart");
}
else if (buttonIndex == 1){
NSLog(#"Exit");
}
}
Here is the drbugging message when i press a button from the second Alert view:
2013-09-11 17:12:06.106 Diplomatiki[2808:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'textFieldIndex (0) is outside of the bounds of the array of text fields'
* First throw call stack:
(0x1c42012 0x1737e7e 0x1c41deb 0xa7f0f0 0xb5d0 0xa72020 0x174b705 0x682920 0x6828b8 0x743671 0x743bcf 0x742d38 0x6b233f 0x6b2552 0x6903aa 0x681cf8 0x1fc2df9 0x1fc2ad0 0x1bb7bf5 0x1bb7962 0x1be8bb6 0x1be7f44 0x1be7e1b 0x1fc17e3 0x1fc1668 0x67f65c 0x29fd 0x2925 0x1)
libc++abi.dylib: terminate called throwing an exception
I will apreciate any suggestions. Thank you in advance.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
This is error, your second alertView doesn't contain any textField. As in documentation says:
Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field.
This is a solution that should work
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101 && buttonIndex == 0) {
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
...
}
Crash is due to your second alert view don't have a textfield. But you are trying to access the textfield and it's value from the clickedButtonAtIndex:regardless which alerview is clicked.
Implement like: clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 101)
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];
NSLog(#"%d", setTime);
if( buttonIndex == 0)
{
NSLog(#"Ok");
[self setTimer];
[self countDownTimerPrepare];
[self countDownTimerIntro];
[self prepareForIntroAnimation];
[self performIntroAnimation];
[self categoriesList];
}
else if (buttonIndex == 1)
{
NSLog(#"Cancel");
ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:#"ChooseViewController" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
else if (alertView.tag==102)
{
if(buttonIndex == 0)
{
NSLog(#"Restart");
}
else if (buttonIndex == 1)
{
NSLog(#"Exit");
}
}
}
Axel, the same problem as in previous case
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
if (alertView.tag == 101){
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 ){
return YES;
}
else{
return NO;
}
}else{
return NO;
}
}
The selector 'go' with IBAction return type is not responding correctly. If the button is clicked with the text field being empty i.e its value being nil , the flow should return the 'if' part and not 'else'. But it works fine when I click the button second time. what can be the problem ?
Below is the code from implementation file i.e. ViewController.m where I have implemented the go selector.
#synthesize textField = _textField;
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
//Clear the text field
self.textField.text =nil;
}
NSString *s;
-(IBAction)go:(id)sender
{
[self.textField resignFirstResponder];
if (self.textField.text == nil)
{
s = [NSString stringWithFormat:#" Enter Your Name First " ];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Blank Field : "
message:s
delegate:self
cancelButtonTitle:#"OKAY"
otherButtonTitles:nil];
[alert show];
}
else
{
s = [NSString stringWithFormat:#"Hello %# " , self.textField.text];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello "
message:s
delegate:self
cancelButtonTitle:#"Thanks"
otherButtonTitles:nil];
[alert show];
}
}
Please help.
Thanks in advance.
Instead of using nil as your comparator I would instead use something more concrete like
if ([self.textfield.text length] > 0)
{
}
else
{
}
I have an error when I run my code: It says undeclared identifier of 'alertView' for this line of code:
-(void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)
else if(butonIndex==1)
}
Thank you for your help.
It goes something like the following.
- (void)dismissPop2:(NSString *)projectname {
projectdelete = projectname;
NSString *msg = [NSString stringWithFormat:#"Are you sure you want to delete this project (%#)?",projectname];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"- Project deletion -"
message:msg
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Delete", nil];
[alert setTag:100];
[alert show];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { // Validation
if ([alertView tag] == 100) {
return YES;
}
else {
return YES;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 100) {
// Deleting a project
if (buttonIndex == 1) {
// Do something since the user has tapped YES
}
}
If it doesn't help, then you should show more work.
You likely have a syntax error earlier in your code that is breaking the parser. Check to make sure the function above the one you posted is closed with a }, all [] are matched, and statements end with a ;
If you post a bit more, someone could probably point it out, but I'm sure you can find it by reading your code.