我想在导航堆栈中出现的每个 View Controller 上都有一个关闭按钮。我读过 here 说我需要创建一个 uinavigationdelegate 对象,我认为这个对象会有一个像 didTapCloseButton 这样的方法?

问题:
我是否应该创建一个协议(protocol)并确认所有内容,即:

protocol CustomDelegate: UINavigationControllerDelegate {
   func didTapCloseButton()
}

public class ViewController: CustomDelegate {
   func didTapCloseButton() {
     //not sure what goes in here?
   }
}

如何让关闭按钮显示在每个 View 的导航栏上?
当用户单击关闭按钮时,我如何才能关闭该堆栈上的每个 View ?

谢谢你的帮助!

最佳答案

这里有一个简单的解决方案。创建 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
    }
}

关于ios - 为 uinavigationcontroller 堆栈上的每个 View Controller 创建关闭按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42229322/

10-11 22:26
查看更多