在numberPad中添加Done / Next

时间:2016-12-08 15:28:52

标签: ios swift uibarbuttonitem

我已阅读有关如何创建这些按钮的帖子,但他们使用标签系统来识别textFields。我正在使用原型单元格来创建我的textFields

func addDoneButtonOnKeyboard(_ hasNextBtn: Bool = false) -> UIView {
    let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 163, width: 106, height: 53))
    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(doneAction))
    let next: UIBarButtonItem = UIBarButtonItem(title: "Next",
                                                style: .plain,
                                                target: self,
                                                action: #selector(nextAction))

    var items = [UIBarButtonItem]()
    items.append(flexSpace)
    if hasNextBtn {
        items.append(next)
    } else {
        items.append(done)
    }

    doneToolbar.items = items
    doneToolbar.sizeToFit()

    return doneToolbar
}

@objc func doneAction() {
    self.view.endEditing(true)
}

@objc func nextAction(_ textField: UITextField) -> Bool {
    let indexPath = IndexPath(row: index, section: 2)

    // if textField.returnKeyType == .next {
    if textField.inputAccessoryView == next as! UIView? {
        guard let table = tableView, let ip = indexPath,
            let cell = table.cellForRow(at: IndexPath(row: ip.row + 1, section: ip.section)) as? FormFieldTableViewCell
            else { return true }

        return cell.becomeFirstResponder()
    }

    return true
}

1 个答案:

答案 0 :(得分:0)

您可以更改键盘上的更改按钮类型,如下所示, //在cellForRowAtIndexPath方法中分配

yourTextField.returnKeyType = .Next 
txtField.returnKeyType = .Done
txtField.returnKeyType = .Go

以下还有更多,

case Default
case Go
case Google
case Join
case Next
case Route
case Search
case Send
case Yahoo
case Done
case EmergencyCall

您可以在UITextFieldDelegate中根据需要处理操作。