UIALertView multiple buttons with text
Asked Answered
D

4

5

I have an alertview with 3 buttons and text(label and NOT textView) If I jam it all together, it'll become ugly with this tiny scroll-text and all the buttons taking up most of the space. Does anyone know how to fix this?

Dibru answered 11/1, 2013 at 17:50 Comment(0)
M
7

Why don't you try to create a custom View for this.

You can use as you want to customize, size, color, background etc.

And show it as a modal window/view.


If you still want to use alertView then you can give spacing as:

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Title" message:@"\n\n!\n\n\n\n" delegate:self cancelButtonTitle:@"Button 1" otherButtonTitles:@"Button 2",@"Button 3", nil];
[alert show];
Meninges answered 11/1, 2013 at 17:54 Comment(9)
Thanks for a great answer, but since I'm new to Objective-C, I haven't started using multiple views, but still great answer (wish I could vote up..)Dibru
I tried it but the thing is that I need to have to display some information, but thanksDibru
Some information where in place of ! ? And where you want to put ! ?Meninges
You have only two places to show your texts (title and message).Meninges
Yes, sorry for my non-clearness, I meant to display a message (not title) And btw, sorry for not understanding that "!" was supposed to be my messageDibru
In place of message post put your message. If it is of shorter length, use \n\n <your message> \n\n\n\n as many times till you get your desired alerview.Meninges
What happened? no reply? and u accepted voted and again down voted?Meninges
Sorry, I didn't have time yesterday, but it was the solution, thanks! (and btw, I cannot vote up/down, so it wasn't me)Dibru
to vote up, you need 15 points... edit some questions and answers, you will get 2 points for each edit-post. and Now i am giving u +5. and when u cross 15 do vote :)Meninges
B
5

A simple way to move the text view down is to add a message

[alertViewObject setMessage:@"\n"];

THe reason for your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.

Customize your AlertView by using following Code.

// Customize Alert View
UIAlertView *alertView = [UIAlertView new];
alertView.title = @"Alert Title";


// Adding Your Buttons
[alertView addButtonWithTitle:@"Button1"];
[alertView addButtonWithTitle:@"Button2"];
[alertView addButtonWithTitle:@"Button3"];
[alertView addButtonWithTitle:@"Button4"];


// Add a Space for Text View
alertView.message = @"\n";


// View heirarchy, set its frame and begin bounce animation
[alertView show];


// Set the frame
CGRect frame = alertView.frame;
frame.origin.y -= 100.0f;
alertView.frame = frame;


// Adding TextField
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[alertView addSubview:text];
[text becomeFirstResponder]; 

I hope this will help you.

Bumpkin answered 11/1, 2013 at 18:4 Comment(1)
This looks great, is perfect, but I need to have a pre-written message, but I might still use it in another situation. Thanks for taking the time and answering!Dibru
J
0

I suggest that you make two uiaertviews and then use

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

to check which alert view was clicked. If you really need an uialertview, else the answer below is also great.

Detailed:

  1. Add a uialertview delegate to your view controller:

    @interface ViewController : UIViewController <UIAlertViewDelegate>    
    
  2. When creating the 2 alertViews it should look like this:

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Message" message:@"the first UIAlertView" delegate: self cancelButtonTitle:@"Back" otherButtonTitle:@"More Options", nil];
    

and the second:

    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"More Options" message:nil delegate:self cancelButtonTitle:@"Back" otherButtonTitle:@"Option 1", @"Option 2", @"Option 3", nil];
  1. In your ViewController.m add:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    
    if (alertView == alert1) {
      [alert2 show];
    }
    
    else if (alertView == alert2) {        
    
    NSString *string = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([string isEqualToString:@"Option 1"]) {
    
        //Do stuff
    
    }
    else if ([string isEqualToString:@"Option 2"]) {
    
        //Do something else
    }
    else if ([string isEqualToString:@"Option 3"]) {
    
        //Do something 3rd
    }
    }
    
Josh answered 11/1, 2013 at 17:57 Comment(0)
C
0

UIAlertView was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    alertController.addAction(UIAlertAction(title: "AlertAction 0", style: .default, handler: { (alertAction: UIAlertAction) in
        print("0")
    }))

    alertController.addAction(UIAlertAction(title: "AlertAction 1", style: .default, handler: { (alertAction: UIAlertAction) in
        print("1")
    }))

    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertAction: UIAlertAction) in
        print("cancel")
    }))
Copolymer answered 22/1, 2019 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.