MENU
コンテンツ再構築中

Objective-C:UIAlertViewの使い方

リリースにも、開発時のデバッグにも使える UIAlertView の使い方。

INDEX

UIAlertViewの使い方

インスタンス生成時に初期設定を行う
[code]
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”タイトル”
message:@”メッセージ”
delegate:self
cancelButtonTitle:@”Cancel”
otherButtonTitles:@”OK”, nil
];
[aleart show];
[/code]

メソッド

-(NSInteger)addButtonWithTitle:(NSString *)title

ボタンを追加する

[code][alert addButtonWithTitle:@”追加ボタン”];[/code]

-(void)show

アラートを表示する

[code]
[alert show];
[/code]

アラートを自動的に閉じる

[code]
[alert dismissWithClickedButtonIndex:0 animated:NO];
[/code]

プロパティ

delegate (UIAlertViewDelegate)

デリゲートを指定する

[code]alert.delegate = self;[/code]

title (NSString)

タイトルを指定する

[code]
alert.title = @”確認”;
[/code]

message (NSString)

メッセージを指定する

[code]
alert.message = @”実行しますか?”;
[/code]

cancelButtonIndex (NSInteger)

2つめのボタンをキャンセルボタンにする

[code]
alert.cancelButtonIndex = 1;
[/code]

numberOfButtons (NSInteger)

ボタンの数を取得する

[code]int cnt = alert.numberOfButtons;[/code]

デリゲートメソッド

-alertView:

ボタン毎の処理を設定する

[code]
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
NSLog(@”alertButtonIndex:1″);
break;
case 1:
NSLog(@”alertButtonIndex:2″);
break;
}
}
[/code]

alertViewCancel:

AlertView表示中にキャンセル(ホームボタン押下)された時の処理を設定する

[code]
– (void)alertViewCancel:(UIAlertView*)alertView {
NSLog(@”AlertCancel”);
}
[/code]

まとめ

UIAlertView はとても便利なのですが、アラートを表示するためのオブジェクトなので、実行前にユーザーに確認や同意を求めるといった本来の用途以外(例えばリストを表示しユーザーに選択させる等)での使用は、極力控えた方がいいでしょう。
Appleのユーザーインターフェースガイドラインにもこの事は記載されていたと思います。

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX