問題描述:如何在使用者完成某個手勢操作(例如滑動)之後,用 UIAlertView 跳出確認對話框來確認提交動作?以下提供一個可行的解法。
先建立並綁定滑動手勢識別器:
UISwipeGestureRecognizer *gesture1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)];
gesture1.direction = UISwipeGestureRecognizerDirectionRight;
[yourView addGestureRecognizer:gesture1];
當手勢觸發時,建立並顯示 UIAlertView:
-(void)didSwipeLeft:(UIGestureRecognizer *)gestureRecognizer {
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure to commit with its action"
delegate:self cancelButtonTitle:CKString(@"NO") otherButtonTitles:CKString(@"YES"), nil];
[Alert show];
Alert.tag = 222;
Alert.delegate = self;
[Alert release];
}
透過 tag 來辨識是哪一個提示框,再依點擊的按鈕索引執行對應邏輯:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 222) {
if (buttonIndex == 1) {
// 使用者按下 YES,執行提交
} else {
// 使用者按下 NO,取消
}
}
}
透過 tag 標記加上 delegate 的按鈕索引判斷,就能在多個提示框並存時精準地分辨出使用者的選擇,進而決定是否真正執行提交動作。