sample : https://github.com/minjoongkim/UIAlertController-for-ios8
안녕하세요 iOS8에서 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:YES completion: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:YES completion: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.textFie cds.firstObject;
UITextField *password = alertController.textFields.lastObject;
NSLog(@"login = %@", login.text);
NSLog(@"password = %@", password.text);
}];
okAction.enabled = NO;
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion: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:YES completion:nil];
'Developer > iOS' 카테고리의 다른 글
[iOS9] AVPlayerViewController로 비디오 재생하기. Video player (1) | 2016.04.07 |
---|---|
[iOS9] Contacts.framework 연락처, 주소록 불러오기 (2) | 2016.01.08 |
[iOS]iOS9 Sportlight 검색 추가하기 (0) | 2016.01.05 |
[iOS]iOS9 Contacts 이용해 연락처, 주소록 가져오기 (0) | 2016.01.05 |
[iOS] CGContextSetRGBStrokeColor 색깔 Setting하기 (0) | 2015.12.22 |