2013-05-26 43 views
0

我想添加一个“评价这个应用程序”弹出到我的应用程序,目前正在通过UIAlertView看这样做。uialertview添加按钮评级弹出

我有警报显示罚款,标题和取消/完成按钮。

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

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

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

回答

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]; 
} 
+0

它只会在一个地方使用,而您的选项1听起来正确!你可以扩展你的答案点1中的代码片段吗?我试图添加一个按钮到UIAlertView,但有错误。 – Richard

+0

我扩展了这个例子,你得到的错误是什么? –

+0

我现在拥有这一切出色的工作。你的例子和输入是一个很大的帮助,非常感谢!非常感谢! – Richard