Ios 为uinavigationcontroller堆栈上的每个视图控制器创建关闭按钮
我希望在导航堆栈中显示的每个视图控制器上都有一个关闭按钮。我已经读到我需要创建一个uinavigationdelegate对象,我认为这个对象将有一个类似于didTapCloseButton的方法 问题: 我是否应该创建一个协议,并确保所有内容都符合该协议,即:Ios 为uinavigationcontroller堆栈上的每个视图控制器创建关闭按钮,ios,swift,Ios,Swift,我希望在导航堆栈中显示的每个视图控制器上都有一个关闭按钮。我已经读到我需要创建一个uinavigationdelegate对象,我认为这个对象将有一个类似于didTapCloseButton的方法 问题: 我是否应该创建一个协议,并确保所有内容都符合该协议,即: protocol CustomDelegate: UINavigationControllerDelegate { func didTapCloseButton() } public class ViewController:
protocol CustomDelegate: UINavigationControllerDelegate {
func didTapCloseButton()
}
public class ViewController: CustomDelegate {
func didTapCloseButton() {
//not sure what goes in here?
}
}
如何使关闭按钮显示在每个视图的导航栏上?
当用户单击“关闭”按钮时,如何使其关闭该堆栈上的每个视图
谢谢你的帮助 这里有一个简单的解决方案。创建UINavigationController子类并重写pushViewController方法
class NavigationController: UINavigationController {
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
let closeBarButtonItem = UIBarButtonItem(
title: "Close",
style: .done,
target: self,
action: #selector(self.popViewController(animated:)))
viewController.navigationItem.rightBarButtonItem = closeBarButtonItem
}
}
不确定这是否是您想要的,但您可以做到:
protocol CustomDelegate: UINavigationControllerDelegate {
func didTapCloseButton()
}
extension CustomDelegate where Self : UIViewController{
func didTapCloseButton(){
// write your default implementation for all classes
}
}
现在,对于您拥有的每个UIViewController
类,您只需执行以下操作:
class someViewController: CustomDelegate{
@IBAction buttonClicked (sender: UIButton){
didTapCloseButton()
}
}
我相信你可能会感到困惑,实际上有一个由苹果公司实施的UINavigationControllerDelegate[1]协议。[1] :好的,让我编辑我的帖子,以便创建一个从UINavigationDelegate继承的自定义委托。