Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

I'm a Mac

macrumors 6502
Original poster
Nov 5, 2007
436
0
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?
 

Jeremy1026

macrumors 68020
Nov 3, 2007
2,215
1,029
Code:
- (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) {
                 NSLog(@"Stuff");
                 [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

macrumors 6502
Original poster
Nov 5, 2007
436
0
how would I connect that to my UIAlertview initalization method?

Code:
- (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"
							  delegate:self
							  cancelButtonTitle:@"Submit"
							  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];
	}
}
 

I'm a Mac

macrumors 6502
Original poster
Nov 5, 2007
436
0
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?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.