0

我想在我的应用程序中添加一个“评价这个应用程序”弹出窗口,目前正在考虑通过 UIAlertView 执行此操作。

我的警报显示正常,带有标题和取消/完成按钮。

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

我现在需要做的是用 5 个自定义按钮(星号)替换“我的消息”部分。

如何在 uialertview 的中间部分添加一行自定义按钮?!

4

1 回答 1

1

你有两个选择

  1. 采用[alertView addSubview:[[UIButton alloc] init:...]]

  2. 从 UIAlertView 继承一个新视图并在内部进行

如果它只显示在一个位置,1是一种快速简便的解决方案。您可以为每个按钮设置标签并添加相同的点击事件

// 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];
}
于 2013-05-26T20:40:40.297 回答