« | »

08月05日 

iPhoneアプリ

UIAlertでテキスト編集

よくアラートダイアログ内でパスワードとか入力を求めるのがあるけど、標準ではそんな事出来ないので、中にUITextField仕込んであげましょう、ということで。

C:
  1. UIAlertView *alert = [[[UIAlertView alloc] init] autorelease];
  2.     alert.title = @"編集";
  3.     alert.message = @"編集してください。\n\n\n";
  4.     alert.delegate = self;
  5.    
  6.     UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 70.0, 245.0, 25.0)];
  7.     [field setBackgroundColor:[UIColor whiteColor]];
  8.     [alert addSubview:field];
  9.    
  10.     [alert addButtonWithTitle:@"キャンセル"];
  11.     [alert addButtonWithTitle:@"実行"];
  12.     alert.cancelButtonIndex = 0;
  13.    
  14.     [alert show];
  15.    
  16.     [field becomeFirstResponder];
  17.     [field release];

んで、クリック後の処理は…

C:
  1. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  2.     if (buttonIndex != alertView.cancelButtonIndex) {
  3.         for (UIView *subView in alertView.subviews) {
  4.             if ([subView isKindOfClass:[UITextField class]]) {
  5.                 NSString *text = [(UITextField *)subView text];
  6.             }
  7.         }
  8.     }
  9. }

UITextFieldが1個しか追加されてない事が前提のテキスト取得方法ですけどね〜

現状の構成がUITabControllerの上にUIViewControllerが3つ載ってて、うち2つがUINavigationControllerでかつUITableViewという感じになってます。
Appleのガイドラインに引っかかってしまわないかと戦々恐々ではあるんですが、上手い事行ったらそこら辺のTipsも出していければ。

さ〜申請はいつになるかな〜

コメント & トラックバック

コメントなし

コメントフィード

コメント