如何根据 swift 中的标签禁用 UIBarButtonItem?

How to disable UIBarButtonItem based on tag in swift?

我在工具栏中创建了按钮并为其设置了标签。我已经在本地声明了那个按钮。现在在其他一些功能中,我想根据条件禁用按钮。

有什么方法可以在不全局声明按钮的情况下基于标签禁用 UIBarButton。

****************************************************
     func setToolBar()
        {
            let toolbar : UIToolbar = UIToolbar()
            toolbar.sizeToFit()

            let prevButton = UIBarButtonItem(title: "<", style: .Plain, target: self, action:"goBack:")
            prevButton.tag = 20

           let nextButton = UIBarButtonItem(title: ">", style: .Plain, target: self, action:"goNext:")
            nextButton.tag = 30

            let flexButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

            let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action:"done:")

            let arrItems : NSArray = [prevButton , nextButton,flexButton, doneButton]
            [toolbar.setItems(arrItems as? [UIBarButtonItem], animated: true)]
            city.inputAccessoryView = toolbar
            birthdate.inputAccessoryView = toolbar
        }

****************************************************

 func goBack(sender: UIBarButtonItem)
    {
        let prevTag : NSInteger = CurrentTextFiled.tag - 1

        let nextResponder = CurrentTextFiled.superview?.viewWithTag(prevTag)

        if((nextResponder) != nil)
        {
            nextResponder!.becomeFirstResponder()
        }
        else
        {
           //Disable UIBarButton here with tag 20
        }
    }

****************************************************

    func goNext(sender: UIBarButtonItem)
    {
        let prevTag : NSInteger = CurrentTextFiled.tag + 1
        let nextResponder = CurrentTextFiled.superview?.viewWithTag(prevTag)

        if((nextResponder) != nil)
        {
            nextResponder!.becomeFirstResponder()
        }
        else
        {
           //Disable UIBarButton here with tag 30
        }
    }

UIView 的实例有一种根据标签查找子视图的方法:

func viewWithTag(_ tag: Int) -> UIView?

我很确定这会进行深入搜索,因此,只要您可以到达层次结构顶部附近的视图,它就应该适合您。

您可以查看 using 标签。

    if let button = self.view.viewWithTag(YOUR_TAG_HERE) as? UIBarButtonItem {
        button.enabled = false
    }

我将其提炼成可以在游乐场 运行 中使用的代码片段,并在 Xcode 中验证了这一点。

本质上,你想给工具栏一个标签,用viewWithTag()找到它。拥有工具栏后,遍历其项目数组并过滤出要操作的按钮。不要忘记将工具栏作为子视图添加到 viewController 的视图中。

import UIKit
import XCPlayground

class MyViewController: UIViewController {

override func viewDidLoad() {
   self.setToolBar()
   self.disableButton(20)
}

func setToolBar()
{
    let toolbar : UIToolbar = UIToolbar()
    toolbar.tag = 10
    toolbar.sizeToFit()

    self.view.addSubview(toolbar)  //must add to subview of viewcontroller

    let prevButton = UIBarButtonItem(title: "<", style: .Plain, target: self, action:"goBack:")
    prevButton.tag = 20

    let nextButton = UIBarButtonItem(title: ">", style: .Plain, target: self, action:"goNext:")
    nextButton.tag = 30

    let flexButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

    let doneButton = UIBarButtonItem(title: "Done", style: .Plain, target: self, action:"done:")

    let arrItems : NSArray = [prevButton, nextButton,flexButton, doneButton]
    [toolbar.setItems(arrItems as? [UIBarButtonItem], animated: true)]
    print("Toolbar set.")
}

func disableButton(tag: Int)
{
    if let toolbarWithButtons = self.view.viewWithTag(10) as? UIToolbar {
        print("Toolbar found by tag. Trying to disable button with tag \(tag).")
        var buttonToDisable: Array<AnyObject>?
        if let buttons = toolbarWithButtons.items {
            buttonToDisable = buttons.filter({
                (x : AnyObject) -> Bool in

                if let button = x as? UIBarButtonItem {
                    if button.tag == tag {
                        return true
                    }
                }
                return false
            })

            if let button = (buttonToDisable!.first as? UIBarButtonItem){
                button.enabled = false
                print("Button with tag \(button.tag) enabled: \(button.enabled)")
            }
        }
    }
    else {
        print("Toolbar not found by tag.")
    }
}

}


var ctrl = MyViewController()

XCPShowView("Playground VC", ctrl.view)

希望对您有所帮助!

如果有人还需要这个: 如果你有 navigationController,那么你可以像这样访问你的 UIBarButtonItem

    if let barButtonItem = navigationItem.rightBarButtonItems?.first(where: {[=10=].tag == <YOUR_TAG> }) {
        // Do staff here
    }

如果 toolBar 情况类似:

     if let barButtonItem = toolBar.items?.first(where: {[=11=].tag == <YOUR_TAG>}) {
        // Do staff here
     }