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

Discussion in 'iPhone/iPad Programming' started by I'm a Mac, Feb 18, 2009.

  1. macrumors 6502

    Joined:
    Nov 5, 2007
    #1
    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?
     
  2. macrumors 68000

    Joined:
    Nov 3, 2007
    #2
    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];
            }
    
    }
    
     
  3. thread starter macrumors 6502

    Joined:
    Nov 5, 2007
    #3
    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];
    	}
    }
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I believe it's already connected, via the delegate:self directive you're using there.
     
  5. thread starter macrumors 6502

    Joined:
    Nov 5, 2007
    #5
    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?
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     

Share This Page