CustomView始终返回nil

CustomView始终返回nil

本文介绍了将CustomView添加到navigationItem之后,CustomView始终返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码成功地将一个customView添加到navigationItem,但是当尝试访问customView时,它总是返回nil

Bellow code is adding a customView to navigationItem successfully, but when trying to access the customView it always return nil

override func viewDidLoad() {
    super.viewDidLoad()
    let customView = getCustomView() // supposed that the function return a custom view
    let actionButton = UIBarButtonItem(customView: customView)
    self.navigationItem.rightBarButtonItem = actionButton // successfully added customView
    print(navigationItem.rightBarButtonItem?.customView)  // print always nil
}

结果:

nil

推荐答案

我发现访问customView(自定义rightBarButtonItem)的最佳方法是,我们必须通过Swift标准方式进行访问:

I found out that the best way to access our customView (custom rightBarButtonItem), we have to access through Swift standard way:

添加customView后,我们只能通过self.navigationItem.rightBarButtonItems数组访问customView.

After adding the customView, we can access the customView via : self.navigationItem.rightBarButtonItems array only.

就我而言,要从navigationItem取回customView:

In my case to get customView back from navigationItem :

这篇关于将CustomView添加到navigationItem之后,CustomView始终返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:21