我创建了CustomNavigationController并添加了一些常用方法来向其中添加UIBarButtonItems。现在,我想从我的各种viewControllers中调用这些方法。
所以我的问题是-如何从任何其他viewController调用属于customNavigationController的方法。
我可以通过以下方式在 objectiveC 中实现此目标:
[(CustomNavigationController *)self.navigationController addLogoutButtonWithVC:self actionLogoutHandler:@selector(actionLogoutHandler)];
其中“ addLogoutButtonWithVC ”是方法,该方法属于CustomNavigationController。
我正在Swift的上述几行中尝试过,但没有运气。
[注意:嵌入时,我已经用情节提要中的CustomNavigationController替换了NavigationController,所以现在所有的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)
}
在这方面的任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
它看起来应该像:
if let nav = self.navigationController as? CustomNavigationController {
nav.addLogoutButton(withVC: self,
actionLogoutHandler:#selector(CustomNavigationController .actionLogoutHandler(_:))
}
答案 1 :(得分: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)
}
答案 2 :(得分:0)
尝试一下:
guard let customNavigationController = self.navigationController as? CustomNavigationController else { return }
customNavigationController.addLogoutButtonWithVC()