LoginSignup
0
0

More than 5 years have passed since last update.

Block化進化したUIAlertView

Posted at

iOS 7まで、提示や警告また簡単な選択や入力などの目的で、UIAlertViewを使うことは当然です。今はiOS 8とともにUIAlertControllerが後継者といて出現したが、古いシステムバージョンの対応のためUIAlertViewが捨てられUIAlertControllerに切り替えることはできません。では、なぜその機能に切り替えることが必要でしょうか。個人的に見ると、delegateを使うUIAlertViewはblockを代わりに使うUIAlertControllerに、delegateに依頼することで、複数のUIAlertViewが一つのdelegateを共用すると、みな一つの方法で処理し、コードがわかりにくいとなる可能性があります。

UIAlertControllerはblockを利用してその不便を避けます。つまり、その展示する画面と動作は独立し、ほかの実体に関わりがなくなります。利点がわかっているうえで、旧時代のUIAlertViewにそれをつけばいいではないかという点がでます。

結果として、その考え方を実現しました。Block化進化したUIAlertViewとして、SinriAlertViewを実装してテストを行いました。実用できるので発表しました。ソースコードはそのリンクにあります。リンクは、
http://www.everstray.com/news/news.php?newsCode=MjAxNTAyMDkxMzM2MjVbU2lucmlBbGVydFZpZXcsIEJsb2NrLUJhc2VkIEFsZXJ0IFZpZXcgZm9yIGlPU11FdmVyc3RyYXkubG9

使用例は以下の通りです。普通の例は含められると思って、入力するための例を提供しました。

SinriAlertView * sav=[[SinriAlertView alloc]initWithTitle:NSLocalizedString(@"_CashingCoupon_InputBox_Title", @"Cashing Coupon")
                                                  message:NSLocalizedString(@"_CashingCoupon_InputBox_Message", @"Input your coupon code to cashing.")
                                        cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
                                        otherButtonTitles:NSLocalizedString(@"Confirm", @"Confirm"),nil];
[sav setClickHandler:^(UIAlertView *sinriAlertView, NSInteger buttonIndex) {
    NSLog(@"SinriAlertView ボタン index=%ld",buttonIndex);
    if (buttonIndex == 1) {
        UITextField *tf=[sinriAlertView textFieldAtIndex:0];
        NSString* cashCode = tf.text;
        NSLog(@"SinriAlertView コード:%@",cashCode);
    }
}];
sav.alertViewStyle = UIAlertViewStylePlainTextInput;
[[sav textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[sav textFieldAtIndex:0] becomeFirstResponder];
[sav show];

つまり、できる限り利点を利用しましたと思います。また何か補足が教えていただけると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0