9

I've added a UIAlertView in my application that grabs user input but I'm unsure as to how I can add a third button. Ideally the three buttons would be across the alert horizontally or two would be above the "cancel" button. The code snippet below is what I'm using to add the UIAlertView.

- (IBAction)initiateSave{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Archive" 
                                          message:@"Enter a name to save this as:" 
                                          delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Save Session",@"Save",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = UIKeyboardTypeDefault;
    alertTextField.placeholder = @"eg. My awesome file...";
    alert.tag = 1;
    [alert show];
    [alert release];
    self.name = [[alert textFieldAtIndex:0]text];
}

enter image description here

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
erran
  • 1,300
  • 2
  • 15
  • 36

6 Answers6

2

It's works fine to me. After showing lot of thread finally i found solution

UIAlertView* getWebUrl = [[UIAlertView alloc] initWithTitle:@"Enter URL" 
                                              message:nil 
                                              delegate:self 
                                              cancelButtonTitle:@"Cancel" 
                                              otherButtonTitles:@"Save", 
                                                                @"Save Url", nil];
getWebUrl.transform=CGAffineTransformMakeScale(1.0, 0.75);
getWebUrl.alertViewStyle=UIAlertViewStylePlainTextInput;
[getWebUrl show];

and align buttons and textfield after present alertview

-(void)willPresentAlertView:(UIAlertView *)alertView {
    for (UIView *view in alertView.subviews) {
        if ([view isKindOfClass:[UITextField class]]||
            [view isKindOfClass:[UIButton class]] || view.frame.size.height==31) {
            CGRect rect=view.frame;
            rect.origin.y += 65;
            view.frame = rect;
        }
    }
}
sschrass
  • 7,014
  • 6
  • 43
  • 62
2

This can definitely be achieved, first of all you'll need to set up something like this.

// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = @"Find";
// Add Buttons
[av addButtonWithTitle:@"Cancel"];
[av addButtonWithTitle:@"Find & Bring"];
[av addButtonWithTitle:@"Find & Go"];
[av addButtonWithTitle:@"Go to Next"];
// Make Space for Text View
av.message = @"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];

QUOTED FROM... https://stackoverflow.com/a/412618/716216

Then you'll want to animate moving the UIAlertView up when the keyboard is called up... something like this...

-(void)keyboardWillShow: (id) notification {

if(showAlert==YES)
{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)];
    [createNewAlert show];
    [UIView commitAnimations];
}
}

-(void)keyboardWillHide: (id) notification {

if(showAlert==YES) 
{


        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)];

        [UIView commitAnimations];

}
}

QUOTED FROM... https://stackoverflow.com/a/3844956/716216

Of course you can find additional info on custom UIAlertViews in the following Apple sample code! https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html

Good Luck!!!!

Community
  • 1
  • 1
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thanks for the info, the other answers weren't really valid when using 3 buttons. I tested the code but it was still tightly packed. I ended up creating a separate alert for saving sessions. This answer led me to a lot of helpful information on `UIAlertViews` in general. – erran Mar 19 '12 at 23:15
  • Good to hear, if you feel like this answered your question please mark it as correct for everyone else's reference. – Mick MacCallum Mar 20 '12 at 00:24
2

Apple really doesn't want you messing with UIAlertView. If the way it naturally formats itself doesn't meet your needs, consider putting up a custom presented ("modal") view instead.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • My answer isn't the only possible right answer, but it is still a right answer. You're really not supposed to do what the questioner wants to do. – matt Sep 22 '12 at 15:23
1

I don't think you can manually resize a UIAlertView, but a trick I use for including a UIActivityIndicatorView is to use "/n" in the message string to make the "message" larger (one extra line per each "/n"), therefore making enough space for everything else.

Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
1

This is somewhat tricky, Here is the solution for this.You need to use \n in the message string to increase the height of the UIAlertView.

- (IBAction)initiateSave{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Archive" 
                                          message:@"\n\n\n\n\n"  // Trick to increase the height
                                          delegate:self 
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"Save Session",@"Save",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

After increasing the height of Alert, now you can add a label to set the message in the label : "Enter a name to save this as:" at appropriate  position.

[alert addSubView: messageLbl];
    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = UIKeyboardTypeDefault;
    alertTextField.placeholder = @"eg. My awesome file...";
    alert.tag = 1;
    [alert show];
    [alert release];
    self.name = [[alert textFieldAtIndex:0]text];
}
Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
  • 1
    Hi man, the "\n" only works with two buttons, once more than two buttons, that cannot change the alertview's frame – Paradise Jun 22 '13 at 04:43
0

Here, a bit late but should do the trick. (Work for me). Create UIAlertView

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TITLE"
                                             message:@"BODY"
                                            delegate:self
                                   cancelButtonTitle:@"CANCEL"
                                   otherButtonTitles:@"OK",@"SEARCH",nil];

    alert.tag = kAlertTag;
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField *alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = UIKeyboardTypeDefault;
    alertTextField.placeholder = @"ENTER STH";
    [alert show];
    [alert release];

And implement delegate method.

-(void)willPresentAlertView:(UIAlertView *)alertView {
    if (alertView.tag == kAlertTag) {
        [alertView setFrame:CGRectMake(17, 30, 286, 188)];
        NSArray *subviewArray = [alertView subviews];
        UILabel *messageLB = (UILabel *)[subviewArray objectAtIndex:2];
        [messageLB setFrame:CGRectMake(10, 46, 260, 20)];
        UIButton *cancelBT = (UIButton *)[subviewArray objectAtIndex:3];
        [cancelBT setFrame:CGRectMake(10, 130, 100, 42)];
        UIButton *okBT = (UIButton *)[subviewArray objectAtIndex:4];
        [okBT setFrame:CGRectMake(194, 130, 80, 42)];
        UIButton *searchBT = (UIButton *)[subviewArray objectAtIndex:5];
        [searchBT setFrame:CGRectMake(112, 130, 80, 42)];
        UITextField *plateTF = (UITextField *)[subviewArray objectAtIndex:6];
        [plateTF setFrame:CGRectMake(10, 80, 266, 50)];
        UITextField *placeTF = (UITextField *)[subviewArray objectAtIndex:7];
        [placeTF setFrame:CGRectMake(15, 70, 256, 50)];
    }
}

FYI [subviewArray objectAtIndex:1] is for title of alertview.

majorl3oat
  • 4,327
  • 1
  • 18
  • 21