I have a counter in my app that has a reset button, but instead of resetting immediately after tapped, I want a UIAlertView to popup and have the user tap Reset again as a button in the alert. I'm not entirely well experienced in this so I would ask that your answer just simply add/replace parts of my code below. I'm working on this lol. Thanks for any help!
- (IBAction)reset {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"titleHere"
message:@"messageHere"
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset", nil];
alert.tag = TAG_RESET;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
} else if (alertView.tag == TAG_RESET) { // i need help here
}
}
So basically just an IBAction within an alert.
UPDATE: How would I incorporate this to the button?
-(IBAction)zero {
counter=0;
count.text = [NSString stringWithFormat:@"%i",counter];
}
UPDATE2:
So I did this, which now clears the count perfectly, the only issue is now the alert continues to pop up after you tap either cancel or reset...
-(IBAction)resetButtonPushed {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"titleHere"
message:@"messageHere"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Reset", nil];
alert.tag = TAG_RESET;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == TAG_DEV){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite.com"]]; // just another alert don't worry about this one
}
else if (alertView.tag == TAG_RESET) { [self resetButtonPushed]; {
counter=0;
count.text = [NSString stringWithFormat:@"%i",counter];
} if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"Reset"]){
// Reset button tapped
}
}
}
I would advise against marking your
zero
function asIBAction
when you're looking over your code later it will be confusing.The naming of your reset method could also be confusing. I would suggest a more descriptive name. And also use of the standard
IBAction
method signature of taking in a sender parameter. Like so:It would seem you are familiar with the
UIAlertViewDelegate
paradigm of waiting for a callback. Since you are using-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
you would use theclickedButtonAtIndex:(NSInteger)buttonIndex
parameter passed into the delegate method. Like so:Alternatively you could do something like:
This section in response to comment
I'm not sure why you're having difficulty understanding how to call your
zero
method. I assume it's because the termIBAction
is unnecessarily distracting you.IBAction
is another way of sayingvoid
. The only difference is that when you useIBAction
interface builder "sees" the method. As far as code is concerned the following two method definitions are identical.Again I would also advise against the
IBAction
and for a more descriptive method name. Perhaps something like:You would of course call this method just like any other, to use it in the context from the above example.
Response to "edit 2" Of course it shows another alert view, you ask it to with the
[self resetButtonPushed]
call in the alertview delegate method.This edit uses the code currently in you question.