2013-05-26 40 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