Skip to content

Instantly share code, notes, and snippets.

@Heilum
Created October 24, 2018 10:44
Show Gist options
  • Save Heilum/820b741d49bb3c5df64e2c352e4b8b6a to your computer and use it in GitHub Desktop.
Save Heilum/820b741d49bb3c5df64e2c352e4b8b6a to your computer and use it in GitHub Desktop.
//
// UIViewController+SF.swift
// BowenEdu
//
// Created by CHENWANFEI on 2018/8/21.
// Copyright © 2018 swordfish. All rights reserved.
//
import UIKit
extension UIViewController{
@objc private func onInputTextFieldEditing(tf:UITextField){
let alertController = self.presentedViewController as! UIAlertController
let saveAction = alertController.actions.first!
if let text = tf.text?.trimed,text.count > 0{
saveAction.isEnabled = true
}else{
saveAction.isEnabled = false
}
}
func popupInputDialog(initString:String?,title:String?,message:String?,placeholder:String?, keyboardType:UIKeyboardType = UIKeyboardType.default, callback:@escaping (String) -> Void){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let saveAction = UIAlertAction(title: "确定", style: .default, handler: { alert -> Void in
let firstTextField = alertController.textFields![0] as UITextField
callback(firstTextField.text!)
})
alertController.addTextField { [weak self](textField : UITextField!) -> Void in
guard let `self` = self else{
return
}
textField.placeholder = placeholder
textField.text = initString
textField.keyboardType = keyboardType
textField.addTarget(self, action: #selector(self.onInputTextFieldEditing(tf:)), for: .editingChanged)
if let text = initString?.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines),text.count > 0{
saveAction.isEnabled = true
}else{
saveAction.isEnabled = false
}
}
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
alertController.addAction(saveAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment