Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2014年09月29日

UIAlertControllerでテキスト入力する



iOS8からDeprecatedとなったUIAlertViewの代わりに利用できるようになったUIAlertControllerでテキスト入力する方法。

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

タグ:apple iPAD IOS iPhone Mac
posted by mobileDeveloper at 16:33 | Comment(0) | TrackBack(0) | UIAlertController はてなブックマーク - UIAlertControllerでテキスト入力する | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。