从Swift中的另一个ViewController访问rightBarButton函数

时间:2019-04-26 07:33:48

标签: ios swift function viewcontroller

我正在尝试访问rightBarButton功能来显示来自另一个ViewController的购物车,尽管我可以调用该功能,但它不会显示任何图像,即购物车图像

下面的代码显示了我如何调用此函数。

override func viewDidLoad() {
            super.viewDidLoad()
            // Adding Right Bar Button Item
            let rightBarButton = RightBarButton()
            rightBarButton.addingRightButton()
    }

下面的代码显示了rightBarButton函数。

class RightBarButton {

    var storyboard = UIStoryboard()
    var navigationItem = UINavigationItem()
    var navigationController = UINavigationController()

    func addingRightButton() {

        let image = UIImage(named: "cart") 

        let finalImage = resizeImage(image: image!, newWidth: 30)

        navigationItem.rightBarButtonItem = FFBadgedBarButtonItem(image: finalImage, target: self, action: #selector(rightButtonTouched))

        let button = navigationItem.rightBarButtonItem as? FFBadgedBarButtonItem

        cartCount { (cartCount) in
            print("Api calling done ...")
            print(cartCount)
            button?.badge = "\(cartCount)"
        }
    }

    @objc func rightButtonTouched() {
        // print("Event Called")
    }

    func cartCount(completion: @escaping (Int) -> Void) {
        // print("Api calling ...")
    }
}

如果我将此代码添加到viewDidLoad()内,图像将显示,并且click事件也将起作用。

如果您发现有关此代码的任何解决方案,请提供帮助!

2 个答案:

答案 0 :(得分:1)

这不起作用,因为您的navigationItem类中的RightBarButton不是任何导航控制器的一部分(因为它是手动创建的,并且未明确附加)。

使创建的FFBadgedBarButtonItem实例可供视图控制器访问,以便您可以正确添加它:

override func viewDidLoad() {
    super.viewDidLoad()
    // Adding Right Bar Button Item
    let rightBarButton = RightBarButton()
    rightBarButton.addingRightButton()

    navigationItem.rightBarButtonItem = rightBarButton.badgeBarItem // badgeBarItem is the property exposed to the view controller
}

编辑过的RightBarButton类:

class RightBarButton {

    var badgeBarItem: FFBadgedBarButtonItem?

    func addingRightButton() {

        let image = UIImage(named: "cart")

        let finalImage = resizeImage(image: image!, newWidth: 30)

        badgeBarItem = FFBadgedBarButtonItem(image: finalImage, target: self, action: #selector(rightButtonTouched))

        cartCount { [weak self] cartCount in
            print("Api calling done ...")
            print(cartCount)
            self?.badgeBarItem?.badge = "\(cartCount)"
        }
    }

    @objc func rightButtonTouched() {
        // print("Event Called")
    }

    func cartCount(completion: @escaping (Int) -> Void) {
        // print("Api calling ...")
    }
}

答案 1 :(得分:1)

您需要使用ViewController检索所需的rightBarButton实例,例如说neededViewController。然后您可以像这里这样称呼它:

neededViewController.viewDidLoad()

或在neededViewController的类中使用其他内容制作另一个方法:

func addRB () {
// let rightBarButton = RightBarButton(image: UIImage(named: "imageName"), landscapeImagePhone: UIImage(named: "imageName"), style: .done, target: self, action: nil)   

   let nc = navigationController
   let navBar = nc?.navigationBar

   let but = UIBarButtonItem(image: UIImage(named: "imageName"), landscapeImagePhone: UIImage(named: "imageName"), style: .done, target: self, action: nil)

// try to create RightBarButton with defined images and a text and pass it instead of `but`
   navBar?.topItem?.setRightBarButton(but, animated: false)
}

并致电neededViewController.addRB()