Swift:在工具栏中添加已完成,下一个,上一个功能

时间:2018-07-25 13:47:21

标签: ios swift

我想添加下一个以前完成的功能,但我不知道。请帮助我,因为我是新手。这是代码:

var inputToolbar: UIToolbar = {
    var toolbar = UIToolbar()
    toolbar.barStyle = .default
    toolbar.isTranslucent = true
    toolbar.sizeToFit()

    var doneButton = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: Selector("doneButtonAction"))
    var flexibleSpaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    var fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)

    var nextButton  = UIBarButtonItem(image: UIImage(named: "1"), style: .bordered, target: self, action:Selector(("nextButtonAction")))
    nextButton.width = 13.0

    var previousButton  = UIBarButtonItem(image: UIImage(named: "2"), style: .bordered, target: self, action: Selector("previousButtonAction"))
    previousButton.width = 13.0
    toolbar.setItems([fixedSpaceButton, nextButton, fixedSpaceButton, previousButton, flexibleSpaceButton, doneButton], animated: true)
    toolbar.isUserInteractionEnabled = true

    return toolbar
}()
func doneButtonAction()
    {
       // here should be the code for Done button
    }
func nextButtonAction()
    {
       // here should be the code for Next button
    }
func previousButtonAction()
    {
       // here should be the code for Previous button
    }
 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

       percentageField.inputAccessoryView = inputToolbar
    valueField.inputAccessoryView = inputToolbar
}

The design is OK now i need actions to be functional
预先感谢。

2 个答案:

答案 0 :(得分:1)

对代码进行了很少的更改。对于inputToolbar,请检查以下代码:

var inputToolbar: UIToolbar = {
    var toolbar = UIToolbar()
    toolbar.barStyle = .default
    toolbar.isTranslucent = true
    toolbar.sizeToFit()

    //Removed bordered because it's deprecated. And corrected all selectors for all buttons
    var doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneButtonAction))
    var flexibleSpaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    var fixedSpaceButton = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)

    //Set image instead of title here
    var nextButton  = UIBarButtonItem(title: "left", style: .plain, target: self, action: #selector(previousButtonAction))
    nextButton.width = 13.0

    var previousButton  = UIBarButtonItem(title: "right", style: .plain, target: self, action: #selector(nextButtonAction))
    previousButton.width = 13.0
    toolbar.setItems([fixedSpaceButton, nextButton, fixedSpaceButton, previousButton, flexibleSpaceButton, doneButton], animated: true)
    toolbar.isUserInteractionEnabled = true

    return toolbar
}()

现在,对于辅助功能,在@objc之前添加func以使#selector正常工作。

检查具有工作功能的更新代码。

@objc func doneButtonAction()
{
    self.view.endEditing(true)
}
@objc func nextButtonAction()
{
    if let selectedRange = percentageField.selectedTextRange {

        // and only if the new position is valid
        if let newPosition = percentageField.position(from: selectedRange.start, offset: 1) {

            // set the new position
            percentageField.selectedTextRange = percentageField.textRange(from: newPosition, to: newPosition)
        }
    }
}
@objc func previousButtonAction()
{
    if let selectedRange = percentageField.selectedTextRange {

        // and only if the new position is valid
        if let newPosition = percentageField.position(from: selectedRange.start, offset: -1) {

            // set the new position
            percentageField.selectedTextRange = percentageField.textRange(from: newPosition, to: newPosition)
        }
    }
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    percentageField.inputAccessoryView = inputToolbar
    return true
}

在您的textFieldShouldBeginEditing方法中,您有两个textField,因此您需要管理点击了哪个textField。并据此可以管理nextButtonActionpreviousButtonAction方法。

在这里您可以看到结果:

enter image description here

here是演示项目,以获取更多信息。

答案 1 :(得分:0)

extension UIViewController {
    func addInputAccessoryForTextFields(textFields: [UITextField], dismissable: Bool = true, previousNextable: Bool = false) {
        for (index, textField) in textFields.enumerated() {
            let toolbar: UIToolbar = UIToolbar()
            toolbar.sizeToFit()

            var items = [UIBarButtonItem]()
            if previousNextable {
                let previousButton = UIBarButtonItem(image: UIImage(named: "prevArrow"), style: .plain, target: nil, action: nil)
                previousButton.width = 30
            if textField == textFields.first {
                previousButton.isEnabled = false
            } else {
                previousButton.target = textFields[index - 1]
                previousButton.action = #selector(UITextField.becomeFirstResponder)
            }

            let nextButton = UIBarButtonItem(image: UIImage(named: "nextArrow"), style: .plain, target: nil, action: nil)
                nextButton.width = 30
            if textField == textFields.last {
               nextButton.isEnabled = false
            } else {
                nextButton.target = textFields[index + 1]
                nextButton.action = #selector(UITextField.becomeFirstResponder)
            }
            items.append(contentsOf: [previousButton, nextButton])
            }

            let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: view, action: #selector(UIView.endEditing))
            items.append(contentsOf: [spacer, doneButton])


            toolbar.setItems(items, animated: false)
            textField.inputAccessoryView = toolbar
        }
    }
}

在您的viewDidLoad()中添加

addInputAccessoryForTextFields(textFields: [textFld1, textFld2, textFld3, textFld4], dismissable: true, previousNextable: true)