UIAlertController *textAlert = [UIAlertController alertControllerWithTitle:@"文字の入力"
message:@"入力して下さい。"
preferredStyle:UIAlertControllerStyleAlert];
[textAlert addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.placeholder = @"text";
}
];
UIAlertAction *keywordOkAction = [UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// ボタンタップ時の処理
NSLog(@"textAlert.textFields.firstObject %@", textAlert.textFields.firstObject);
UITextField *textField = textAlert.textFields.firstObject;
NSLog(@"textField %@", textField.text);
}
];
UIAlertAction *keywordCancelAction = [UIAlertAction actionWithTitle:@"CANCEL"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// ボタンタップ時の処理
}
];
// コントローラにアクションを追加
[textAlert addAction:keywordCancelAction];
[textAlert addAction:keywordOkAction];
// アクションシート表示処理
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// 画面の中心に表示
textAlert.popoverPresentationController.sourceView = self.view;
textAlert.popoverPresentationController.sourceRect = self.view.bounds;
textAlert.popoverPresentationController.permittedArrowDirections = 0; // 吹き出しの矢印を表示しない
}
[self presentViewController:textAlert animated:YES completion:nil];
参考記事:
http://useyourloaf.com/blog/2014/09/05/uialertcontroller-changes-in-ios-8.html
https://gist.github.com/yume190/6f5e3b3c07bececc56d2
【UIAlertControllerの最新記事】