2015-05-31 29 views
0

我想重播一個帶textfield的alertView,直到它被取消。文本字段中的文本應保存在NSMutableArray中。我嘗試了一些,但它不起作用。它會由myNSMutableArray = ... EXPECTED標識符表示。我究竟做錯了什麼?UIAlertView保存textfield.tex

@synthesize myNSMutableArray; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self alert]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:  (NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     myNSMutableArray = [[NSMutableArray alloc] addObject:[[alertView textFieldAtIndex:0].text]]; 
     [self alert]; 
    } 
    else{ 
     NSLog(@"Done"); 
    } 
} 

-(void)alert{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Titel" message:@"Message" delegate:self cancelButtonTitle:@"Next" otherButtonTitles:@"Done", nil]; 

    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

    [[alert textFieldAtIndex:0] setPlaceholder:@"First"]; 

    [alert show]; 
} 

回答

4

你有一些問題和黛咪問題:

取出@synthesize,你應該幾乎從來沒有使用,現在。您需要預先創建數組,而不是重複創建而不是初始化它(從而破壞前一個對象)。當您使用點符號時,文本字段使用的括號過多。

請記住,如果您在編譯代碼時遇到問題,請將其分解爲多個部分,以便您可以看到哪個位確實存在問題,並且您的代碼更易於遵循。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.myNSMutableArray = [NSMutableArray array]; 

    [self alert]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     [self.myNSMutableArray addObject:[alertView textFieldAtIndex:0].text]; 
     [self alert]; 
    } 
    else{ 
     NSLog(@"Done"); 
    } 
} 

-(void)alert { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Titel" message:@"Message" delegate:self cancelButtonTitle:@"Next" otherButtonTitles:@"Done", nil]; 

    alert.alertViewStyle = UIAlertViewStylePlainTextInput; 

    [[alert textFieldAtIndex:0] setPlaceholder:@"First"]; 

    [alert show]; 
} 
+0

謝謝。我很感激。 – Nanog000