2016-07-21 98 views

回答

11
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Text" 
                   message:@"Enter some text below" 
                 preferredStyle:UIAlertControllerStyleAlert]; 

UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction * action) { 

                if (alert.textFields.count > 0) { 

                 UITextField *textField = [alert.textFields firstObject]; 

                 textField.text // your text 
                } 

               }]; 

[alert addAction:submit]; 

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
    textField.placeholder = @"something"; // if needs 
}]; 

[self presentViewController:alert animated:YES completion:nil]; 
1

要添加TextField對UIAlertView設置alertViewStyle屬性與UIAlertViewStylePlainTextInput

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
               message:@"Message" 
               delegate:self 
             cancelButtonTitle:@"Done" 
             otherButtonTitles:nil]; 
alert.alertViewStyle = UIAlertViewStylePlainTextInput; 
[alert show]; 

在h文件添加UIAlertViewDelegate作爲協議和實現在.m文件委託方法alertView:clickedButtonAtIndex

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    NSLog(@"%@", [alertView textFieldAtIndex:0].text); 
}