您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

用 UIAlertView 確認手勢提交動作的實作

編輯:iOS 開發綜合

問題描述:如何在使用者完成某個手勢操作(例如滑動)之後,用 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];
}

在 delegate 中處理使用者選擇

透過 tag 來辨識是哪一個提示框,再依點擊的按鈕索引執行對應邏輯:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if (alertView.tag == 222) {
    if (buttonIndex == 1) {
      // 使用者按下 YES,執行提交
    } else {
      // 使用者按下 NO,取消
    }
  }
}

透過 tag 標記加上 delegate 的按鈕索引判斷,就能在多個提示框並存時精準地分辨出使用者的選擇,進而決定是否真正執行提交動作。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved