2012-02-16 73 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