电话簿中的完成按钮(Swift 4)

时间:2018-01-02 10:22:51

标签: ios swift

我是Swift的新手,我正在创建一个注册表单。我有一个电话文本字段,当我打开电话簿时,我看不到完成或返回按钮,因此我可以使用resignfirstresponder()关闭屏幕键盘。

我使用了以下链接:https://gist.github.com/jplazcano87/8b5d3bc89c3578e45c3e

现在,我得到了完成按钮,但是在按完毕后,我的应用程序崩溃了,因为选择器显示未识别。任何人都可以用其他方式帮助或指导我吗?

提前致谢

这是代码

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {


    @IBOutlet var registerTF: [KaustabhTF]!


    // MARK:-  App LifeCycle

    override func viewDidLoad() {

        super.viewDidLoad()
    }


    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)
    }


    override func viewDidAppear(_ animated: Bool) {

        super.viewDidAppear(animated)
    }


    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)
    }


    override func viewDidDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)
    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
    }


    // MARK:- User Defined

    func  isValidEmail(email: String) -> Bool {

        let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$"

        let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)

        let result = emailTest.evaluate(with: email)

        return result

    }

    func isValidPassword(pass:String?) -> Bool {

        let passwordTest = NSPredicate(format: "SELF MATCHES %@", "(?=.*[A-Z])(?=.*[0-9])(?=.*[a-z]).{8,}")

        return passwordTest.evaluate(with: pass)
    }

    func displayAlert(alertMessage: String) {

        let alertController = UIAlertController(title: "Alert!!", message: alertMessage, preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel,handler: nil))


        self.present(alertController, animated: true, completion: nil)
    }

    func addDoneButtonOnKeyboard()
    {
        let doneToolbar: UIToolbar = UIToolbar()

        doneToolbar.barStyle = UIBarStyle.default

        let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: Selector(("doneButtonAction")))

        var items = [UIBarButtonItem]()

        items.append(flexSpace)

        items.append(done)

        doneToolbar.items = items

        doneToolbar.sizeToFit()

        self.registerTF[5].inputAccessoryView = doneToolbar

    }

    func doneButtonAction()
    {
        self.registerTF[5].resignFirstResponder()
    }

    func isValidPhone(value: String) -> Bool {

        let PHONE_REGEX = "^[0-9]{10}$"

        let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)

        let result =  phoneTest.evaluate(with: value)

        return result
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.view.endEditing(true) //This will hide the keyboard

    }

    let myPickerData =  [String](arrayLiteral: "+91", "+971", "+1", "+121", "+80", "+00")


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return myPickerData.count
    }

    func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return myPickerData[row]
    }

    func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        registerTF[4].text = myPickerData[row]
    }

    func nextTextFieldToFirstResponder(textField: KaustabhTF) {

        if textField.absoluteCount() == 0
        {
            displayAlert(alertMessage: "Please enter the apt value.")
        }

        let senderTag = textField.tag

        if senderTag == 0           // Name
        {
            registerTF[senderTag + 1].becomeFirstResponder()
        }

        else if senderTag == 1      // Email
        {

            if !isValidEmail(email: textField.text!)
            {
                displayAlert(alertMessage: "Please input your valid email address")
            }

            else
            {
                registerTF[senderTag + 1].becomeFirstResponder()
            }
        }

        else if senderTag == 2  // Password
        {

            if !isValidPassword(pass: textField.text)
            {
                displayAlert(alertMessage: "Please input a password with minimum of 8 characters including an upper case character, a lower case character and a digit.")
            }

            else
            {
                registerTF[senderTag + 1].becomeFirstResponder()
            }
        }

        else if senderTag == 3 // Confirm Password
        {

            if !isValidPassword(pass: textField.text)
            {
                displayAlert(alertMessage: "Please input a password with minimum of 8 characters including an upper case character, a lower case character and a digit.")
            }

            else if registerTF[senderTag-1].text != registerTF[senderTag].text
            {
                displayAlert(alertMessage: "Password and confirm password dont match. Please try again")
            }

            else
            {
                textField.resignFirstResponder()
            }
        }

        else if senderTag == 4 //Country Code
        {
            let thePicker = UIPickerView()

            registerTF[4].inputView = thePicker

            thePicker.delegate = self

        }

        else if senderTag == 5 // Phone Number
        {
            self.addDoneButtonOnKeyboard()

            if !isValidPhone(value: textField.text!)
            {
                displayAlert(alertMessage: "Please make sure that the phone number is correct")
            }

                doneButtonAction()


        }

    }

3 个答案:

答案 0 :(得分:2)

替换

Selector(("doneButtonAction"))

#selector(ViewController.doneButtonAction)

完整的代码将是:

let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.doneButtonAction))

@objc方法之前添加doneButtonAction,最终代码为:

@objc func doneButtonAction()
{
    self.registerTF[5].resignFirstResponder()
}

有关详细信息,请参阅THIS

答案 1 :(得分:1)

swift 3.0和4.0 中,您可以使用此代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtNumber: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.addDoneButtonOnKeyboard()

    }

    func addDoneButtonOnKeyboard()
    {
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: 320, height: 50))
        doneToolbar.barStyle = UIBarStyle.default

        let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(doneButtonAction))

        var items = [UIBarButtonItem]()
        items.append(flexSpace)
        items.append(done)

        doneToolbar.items = items
        doneToolbar.sizeToFit()

        self.txtNumber.inputAccessoryView = doneToolbar

    }


    **//button action generate following two way both are working great but use any one**

    @objc func doneButtonAction()
    {
        self.txtNumber.resignFirstResponder()
    }
    @IBAction func doneButtonAction(_ sender: UIButton) {
        self.txtNumber.resignFirstResponder()
    }
}

答案 2 :(得分:1)

此帖子的可能重复项:How to show "Done" button on iPhone number pad

我试图添加重复标记,但是由于某些原因我不能这样做。希望对其他人有帮助。