uialertview添加了用于评级弹出窗口的按钮

时间:2013-05-26 20:28:31

标签: popup uialertview rate

我想在我的应用程序中添加“为此应用评分”,我目前正在通过UIAlertView进行此操作。

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您有两个选择

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

  2. 从UIAlertView继承新视图并在内部执行

  3. 如果它只显示在一个地方,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];
    }