Pregunta

I am wanting to add a "rate this app" pop up to my app, and am currently looking at doing this through a UIAlertView.

I have the Alert showing fine, with title and cancel/done buttons.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate this App"
                                                message:@"My message" delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
[alert show];

What I need to do now is to replace the "my message" section with 5 custom buttons (stars).

How can I add a row of custom buttons to the middle part of the uialertview?!

¿Fue útil?

Solución

You have two options

  1. Use [alertView addSubview:[[UIButton alloc] init:...]]

  2. Inherit a new view from UIAlertView and do it internally

If it is only shown in one spot, 1 is a quick and easy solution. You can setTag for each button and add the same click event

// Interface.h

NSArray *allButtons;

// Implementation.m

UIAlertView *alert = [[UIAlertView alloc] init:...];

UIButton *one = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *two = [UIButton buttonWithType:UIButtonTypeCustom];
...

// Load "empty star" and "filled star" images
UIImage *starUnselected = ...;
UIImage *starSelected = ...;

[one setImage:starUnselected forControlState:UIControlStateNormal];
[one setImage:starSelected forControlState:UIControlStateSelected];
// repeat for all buttons
...

[one setTag:1];
[two setTag:2];
...

[one addTarget:self action:@selector(buttonPressed:) 
     forControlEvents:UIControlEventTouchUpInside];

// repeat for all buttons

allButtons = [NSArray arrayWithObjects:one, two, three, four, five];

// all buttons should subscribe
- (void)buttonPressed:(UIButton)sender 
{
    int tag = [sender getTag]; // The rating value

    for (int i = 0; i < [allButtons length]; i++)
    {
        BOOL isSelected = i < tag;

        [(UIButton)[allButtons objectAtIndex:i] setSelected:isSelected];
    }

    // Set alertTag to store current set one
    // read [alert getTag] when OK button is pressed
    [alert setTag:tag];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top