View Full Version : UIAlertView: How do I trigger another event by having the user press OK?

I'm a Mac
Feb 18, 2009, 06:39 PM
This seems like an idiotic question, but I can't for the life of me get an OK button on a UIAlertview to trigger another event. Is this possible? I know with applescript, you'd have "if button returned is 1 then...." but how would I do this with the UIAlertview class. My alert only has one button, so how would I make it a selector with the target of another event?

Feb 18, 2009, 07:17 PM
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

//If you have two buttons this is the left button, if you only have one button, you don't need the else
if (buttonIndex == 0) {
[self doAction];
//If you have two buttons, this is the right button, if you only have one button, this shouldn't be here
else {
NSLog(@"Other Stuff");
[self doOtherAction];


I'm a Mac
Feb 18, 2009, 11:16 PM
how would I connect that to my UIAlertview initalization method?

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
if (numTaps == 1 ) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Alert Title"
message:@"Alert Message"
otherButtonTitles: nil];
[alert addTextFieldWithValue:@"" label:@"Input"];

// Input field
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocorrectionType = UITextAutocorrectionTypeNo;

[alert show];
[[self performSelector:@selector(presentSheet:)] autorelease];

Feb 18, 2009, 11:31 PM
I believe it's already connected, via the delegate:self directive you're using there.

I'm a Mac
Feb 18, 2009, 11:37 PM
EDIT: Okay I figured it all out. Thank you so much for all of your help.

New Problem: Why does the keyboard spring up twice- one over the other when the alert launches?

Feb 18, 2009, 11:47 PM
sorry, I phrased the question poorly. I meant, from my init method, how do I call on the other method? I wanted to add an if statement in the same method as the alert view init method.
How do you call alertView:didDismissWithButtonIndex: from the init method of your UIAlertView? You don't.

I'd suggest checking out some of the sample apps, such as UICatalog, for more code on how to handle UIAlertViews.