2016-12-26 53 views
0

想要查看是否有方法在拍攝截圖後顯示警報視圖,然後需要與特定字符串匹配的文本輸入。如果用戶沒有輸入正確的文本,則警報視圖不會消失,並提示用戶重試。如果用戶輸入正確的文本,則警報視圖被取消。在截圖時需要顯示提醒並需要輸入文字

回答

2

您可以使用此

let requireTextInput = "require text input" 
// add observer for screen shot  
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil, queue: OperationQueue.main, using: 
     { notification in 

      var inputTextField = UITextField() 

      let textPrompt = UIAlertController(title: nil, message: "require text input", preferredStyle: .alert) 

      textPrompt.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil)) 

      textPrompt.addAction(UIAlertAction(title: "OK", style: .default, handler: { 
       (action) -> Void in 
      // if the input match the required text 

       let str = inputTextField.text 
       if str == requireTextInput { 
        print("right") 
       } else { 
        print("wrong") 
       } 

      })) 

      textPrompt.addTextField(configurationHandler: {(textField: UITextField!) in 
       textField.placeholder = "place Holder" 
       inputTextField = textField 

      }) 

      self.present(textPrompt, animated: true, completion: nil) 

    }) 
+0

偉大的作品!謝謝。出於某種原因,導航欄滑出視圖。任何方式使它不能滑走? – Miles

+0

如果你想保持導航欄。您可以將最後一行更改爲「navigationController?.pushViewController」 –

+0

由於整個視圖被推送到具有空白屏幕和鍵盤的新視圖控制器,似乎不起作用。這是與:「self.navigationController?.pushViewController(textPrompt,animated:false)」 – Miles