안녕하세요
iOS9에서 UIAlertView를 사용하면 경고창이 떠서 UIAlertController에 대한 간단한 코드입니다.
필요하신분들 있으실거 같아서 공유해드릴께요.
1,2,3번은 UIAlertControllerStyleAlert의 샘플이고, 4번은UIAlertControllerStylerActionSheet입니다. 1,2번의 타입에 preferredStyle만바꿔주시면 완성됩니다.
1. 기본 AlertView

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"simpleAlert"
message:@"UIAlertControllerStyleAlert"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YEScompletion:nil];
2. 버튼이 있는 AlertView

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"simpleAlert"
message:@"UIAlertControllerStyleAlert"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:@"Reset"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Reset action");
}];
[alertController addAction:resetAction];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YEScompletion:nil];
3. TextInputAlert

-(IBAction)textFieldAlert:(id)sender{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"TextInputAlert"
message:@"Plane and secure text input"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
[textField addTarget:self
action:@selector(alertTextFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
textField.placeholder = @"LoginPlaceholder";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder =@"PasswordPlaceholder";
textField.secureTextEntry = YES;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
UITextField *login = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
NSLog(@"login = %@", login.text);
NSLog(@"password = %@", password.text);
}];
okAction.enabled = NO;
[alertController addAction:okAction];
[self presentViewController:alertController animated:YEScompletion:nil];
}
- (void)alertTextFieldDidChange:(UITextField *)sender
{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController)
{
UITextField *login = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = login.text.length > 2;
}
}
참고로 TextField가 들어가게 되면 UIAlertControllerStyleActionSheet 타입을 이용할수 없습니다. 만약 이용하게 되면 아래와 같은 에러가 납니다.
*** Assertion failure in -[UIAlertController addTextFieldWithConfigurationHandler:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIAlertController.m:434
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'
4. ActionSheet

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"simpleAlert"
message:@"UIAlertControllerStyleAlert"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:@"Reset"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Reset action");
}];
[alertController addAction:resetAction];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YEScompletion:nil];