【问题标题】:How to access methods of CustomNavigationController from any ViewController?如何从任何 ViewController 访问 CustomNavigationController 的方法?
【发布时间】:2018-07-25 10:10:32
【问题描述】:

我创建了 CustomNavigationController 并添加了一些常用方法来添加 UIBarButtonItems 到它。现在我想从我的各种视图控制器中调用这些方法。

所以我的问题是 - 如何从任何其他 viewController 调用属于 customNavigationController 的方法

我可以通过以下方式在 objectiveC 中实现这一点:

[(CustomNavigationController *)self.navigationController addLogoutButtonWithVC:self actionLogoutHandler:@selector(actionLogoutHandler)];

其中“addLogoutButtonWithVC”是属于CustomNavigationController的方法。

我在 Swift 中尝试了以上几行,但没有运气。

[注意:我已经在 storyboard 中将 NavigationController 替换为 CustomNavigationController,所以现在所有的 navigationControllers 都只指向 CustomNavigationController]

"更新:在 CustomNavigationController 中声明 addLogoutButtonWithViewController 和 actionLogoutHandler"

func addLogoutButtonWithViewController(viewCont : UIViewController , selLogout : Selector)  {
    currentController = viewCont
    var barButtonItem : UIBarButtonItem?
    barButtonItem = UIBarButtonItem(image: UIImage(named: "Logout.png"), style: UIBarButtonItemStyle.plain, target: self, action: Selector("actionLogoutHandler"))

   self.navigationController?.navigationItem.rightBarButtonItem = barButtonItem
}

@objc func actionLogoutHandler()  {
    print("Inside logout")
    currentController?.navigationController?.popToRootViewController(animated: true)
}

非常感谢您在这方面的任何帮助。

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    你应该试试我的代码如下。

    您需要导航控制器的对象并用于设置按钮目标。

        if let navigationcontroller = self.navigationController as? CustomNavigationController {
        navigationcontroller.addLogoutButton(withVC: self,
                            actionLogoutHandler:#selector(navigationcontroller.actionLogoutHandler(_:))
    }
    
        @objc func actionLogoutHandler()  {
        print("Inside logout")
        self.popToRootViewController(animated: true)
    }
    

    【讨论】:

    • 这也不起作用 - 以下是编译器错误:“'#selector' 的参数不引用 '@objc' 方法、属性或初始化程序”
    • 谢谢!!您的回答帮助我访问了自定义类的方法,但没有抑制错误“'#selector' 的参数不引用'@objc' 方法、属性或初始化程序”。这个答案:stackoverflow.com/a/41512372/742815 帮助我摆脱了这个错误。所以我修改了你的代码 sn-p 这对我来说很好,可以帮助遇到同样问题的其他人。
    【解决方案2】:

    它应该看起来像:

    if let nav = self.navigationController as? CustomNavigationController { nav.addLogoutButton(withVC: self, actionLogoutHandler:#selector(CustomNavigationController .actionLogoutHandler(_:)) }

    【讨论】:

    • 谢谢!!这与我的答案非常接近,但是 #selector(actionLogoutHandler(_:)) 这不起作用。在我的 CutsomNavigationController 中,我也将 actionLogoutHandler 标记为 @objC。但是根据您建议的答案,编译器会抛出错误为“使用未解析的标识符'actionLogoutHandler'”
    • 已更新。可以贴actionLogOutHandler的声明吗
    • 请看我更新的问题:我已经从 CustomNavigationController 添加了方法声明
    • 感谢@CZ54 的帮助,但这里调用#selector(CustomNavigationController .actionLogoutHandler(:) 相当于#selector(self.actionLogoutHandler(:))。来自另一个控制器“#selector(nav.methodName)”的访问方法有效。
    【解决方案3】:

    试试这个:

    guard let customNavigationController = self.navigationController as? CustomNavigationController else { return }
    customNavigationController.addLogoutButtonWithVC()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多