2013年9月1日日曜日

UIAlertViewの利用方法

UIAlertViewの使い方を記載します。


【一定時間アラートを表示して、自動で閉じる場合】

//アラートの表示
-(void)alert{
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"アラートのタイトル"
    message:@"ここにメッセージを入力"
    delegate:nil                      //UIAlertViewのデリゲートメソッドが必要な場合は"self"に変更
    cancelButtonTitle:nil    //キャンセルボタンが必要な場合
    otherButtonTitles:nil 
    ];


    //Timer 設定(0.5秒設定)
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(performDismiss:) userInfo:alert repeats:NO];
    [alert show];
}

//Timer終了後にアラートを閉じる
- (void)performDismiss:(NSTimer *)theTimer
{
    UIAlertView *alertView = [theTimer userInfo];
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}



【手動でアラートを閉じる場合】


//アラートの表示

-(void)alert{
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"アラートのタイトル"
    message:@"ここにメッセージを入力"
    delegate:nil                                         //UIAlertViewのデリゲートメソッドが必要な場合は"self"に変更
    cancelButtonTitle:nil                       //キャンセルボタンが必要な場合
    otherButtonTitles:@"OK",nil         //OKボタンが作成される。
];

0 件のコメント:

コメントを投稿