2013年12月12日 星期四

UIAlertView的使用

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"didUpdateToLocation"//設定標題
message:[NSString stringWithFormat:@"%f",_heading.trueHeading ]  //設定警告訊息內文
                                                   delegate:self // 要用那一個物件來處理按鈕的delegate事件
                          
cancelButtonTitle:@"OK"  //cancel按鈕文字的設定
otherButtonTitles: nil]; // 設定其他按鈕title
// 如果要多個其他按鈕 >> otherButtonTitles: @"check1", @"check2", nil];
    
    [alert show];  // alert這個物件秀出來
          alert.tag = 0; //當一個View有多個alertView時,可以用來區分
    [alert release];  // 把物件釋放(MRC環境下)
}



//2秒後關alert view
double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [alert dismissWithClickedButtonIndex:0 animated:NO];

    });


//包成method
-(void)_showAlertViewWithTitle:(NSString*)title content:(NSString*)content showTime:(float)showTime{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:content
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"  //cancel按鈕文字的設定
                                          otherButtonTitles: nil]; // 設定其他按鈕title
    // 如果要多個其他按鈕 >> otherButtonTitles: @"check1", @"check2", nil];
    [alert setTag:0];
    [alert show];  // alert這個物件秀出來
    
    //2秒後關alert view
    double delayInSeconds = showTime;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (showTime != 0.0) {
            [alert dismissWithClickedButtonIndex:0 animated:NO];
        }
    });

}


#pragma marks -- UIAlertViewDelegate --
//用Delegate實現alertView的按鍵功能
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"clickButtonAtIndex:%d",buttonIndex);
}

//AlertView已經消失時的事件
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"didDismissWithButtonIndex");
}

//ALertView即將消失時的事件
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"willDismissWithButtonIndex");
}

//AlertView的取消按鍵的事件
-(void)alertViewCancel:(UIAlertView *)alertView
{
    NSLog(@"alertViewCancel");
}

//AlertView已經顯示時的事件
-(void)didPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"didPresentAlertView");
}

//AlertView即將顯示時的事件
-(void)willPresentAlertView:(UIAlertView *)alertView
{
    NSLog(@"willPresentAlertView");
}  

沒有留言:

張貼留言