ios - 如何从任何 ViewController 访问 CustomNavigationController 的方法?
问题描述
我创建了 CustomNavigationController 并添加了一些常用方法来向其中添加 UIBarButtonItems。现在我想从我的各种视图控制器中调用这些方法。
所以我的问题是-如何从任何其他 viewController 调用属于 customNavigationController 的方法。
我可以通过以下方式在objectiveC中实现这一点:
[(CustomNavigationController *)self.navigationController addLogoutButtonWithVC:self actionLogoutHandler:@selector(actionLogoutHandler)];
其中“ addLogoutButtonWithVC ”是属于 CustomNavigationController 的方法。
我在 Swift 中尝试以上几行,但没有运气。
[注意:嵌入时,我已经在情节提要中将 NavigationController 替换为 CustomNavigationController,因此所有 navigationController 现在都指向 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)
}
非常感谢这方面的任何帮助。
解决方案
它应该看起来像:
if let nav = self.navigationController as? CustomNavigationController {
nav.addLogoutButton(withVC: self,
actionLogoutHandler:#selector(CustomNavigationController .actionLogoutHandler(_:))
}
推荐阅读
- python - Python 中的 array.count() 是否比 list.count() 慢几个数量级?
- mysql - 节点 js 应用程序部署在谷歌云连接上,与不同服务器上的远程 mysql 连接。
- laravel - 使用请求将数据从刀片表单解析到控制器
- mysql - 使用 Group BY 的一个 MySQL 查询获取最小值和最大值
- liferay - Liferay 7 预定的期刊内容不起作用
- c# - Unity3d - 相机旋转平滑“错误”
- c - 为什么数组不接受空终止符?
- javascript - 如何在 ng-repeat 中使用 select 并在 api 调用数据上保持选中的值
- javascript - 悬停时div闪烁
- php - Codeigniter 3 分页:将 $offset 变量添加到视图中