2014-05-18 35 views
7

我需要在我的應用程序的輸入提示,我已經試過這UIAlertView中輸入提示

// Create a new item 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

再經手這樣說:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
// The user created a new item, add it 
if (buttonIndex == 1) { 
    // Get the input text 
    NSString *newItem = [[alertView textFieldAtIndex:0] text]; 
} 
} 

,但它並不像在clickedButtonAtIndex被調用,爲什麼?

親切的問候, 埃裏克

+0

爲什麼c#標記? – vikingosegundo

+0

哦,哈哈 - 對不起。我很習慣在開發windows phone時問這個問題,因爲這只是一個老習慣 – Erik

+0

哦。真的有必要一直倒下來嗎?現在我不能問問題了...我在問,因爲我自己還沒有找到解決方案 – Erik

回答

7

您需要設置委託。

alert.delegate = self; //Or some other object other than self 

或者當你初始化警告:不被稱爲

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" 
               message:@"Enter a name for the item" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Add", nil]; 
0

方法,因爲你沒有設置委託。
將自己作爲委託傳遞給它,以便它獲得對它的引用。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New item" message:@"Enter a name for the item" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 
+0

不得不選擇一個答案,所以我選擇了一個聲望最低的人:) - 但謝謝! – Erik