2012-02-16 55 views
0

我有點卡在UIAlertViewStylePlainTextInput上。 (我的代碼太長,不能發佈到這裏,但這是問題存在的部分。)它可以用於各種工作,但程序不會等到用戶輸入信息才能繼續。 「editName」按鈕不會更改下面代碼中的顯示,但updateDisplay按鈕可以正常工作(如果在按下「updateDisplay」之前按下了「editName」按鈕)。在用戶輸入信息(或取消)之前,我怎樣才能讓所有事情停止。還是有一些其他的領導將帶我到我需要去的地方。 (我一直在將其餘的代碼放入 - (void)alertView中,但這似乎並不是這樣做的正確方法)。UIAlertViewStylePlainTextInput - 程序繼續而無需等待用戶的文本輸入

在此先感謝。

#import "HSTestViewController.h" 

@implementation HSTestViewController 
@synthesize display; 

NSString *newName; 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 

{  
    if (buttonIndex == 1) 
    { 
     newName = [[alertView textFieldAtIndex:0] text]; 
    } 
} 


- (void) getNewName; 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You made the High Score List" 
               message:@"Please enter your name" 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Ok", nil]; 

    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
    [alert show]; 
} 

- (IBAction)editName:(id)sender 
{ 
    [self getNewName];  
    newName = display.text; 
} 

- (IBAction)updateDisplay:(id)sender 
{ 
    display.text = newName; 
} 

@end 

回答

2

將UIAlertViewDelegate添加到類中。並執行下面的委託方法。

確定按鈕索引值將在您的情況下爲1。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex) 
    { 
     // call the method or the stuff which you need to perform on click of "OK" button. 
    } 
} 

希望這會有所幫助。

+0

謝謝。我提出了一些建議,現在我的程序完全按照我的意圖去做。我還添加了UIAlertViewDelegate,這是我以前沒有做過的。 – ShadowDES 2012-02-16 21:03:10

相關問題