【问题标题】:uialertview with added buttons for rating popupuialertview 添加了用于评分弹出窗口的按钮
【发布时间】:2013-05-21 17:36:59
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: popup uialertview rate


    【解决方案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];
    }
    

    【讨论】:

    • 它只会在一个地方使用,你的选项1听起来不错!您可以扩展答案点 1 中的代码 sn-p 吗?我尝试向 UIAlertView 添加一个按钮,但出现错误。
    • 我现在所有这些都运行良好。您的示例和输入是一个很大的帮助,非常感谢!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2014-06-10
    • 2015-05-20
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多