Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format more than two buttons inside an UIAlertView with the UIAlertStylePlainTextInput style?

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

like image 667
erran Avatar asked Mar 17 '12 23:03

erran


3 Answers

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.

like image 144
matt Avatar answered Nov 15 '22 20:11

matt


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!!!!

like image 44
Mick MacCallum Avatar answered Nov 15 '22 20:11

Mick MacCallum


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;
        }
    }
}
like image 30
gksundar789 Avatar answered Nov 15 '22 21:11

gksundar789