按下UINavigationController的后退按钮时执行操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按下UINavigationController的后退按钮时执行操作相关的知识,希望对你有一定的参考价值。

当按下UINavigationController的后退按钮时,我需要执行一个动作(清空一个数组),而按钮仍然会导致堆栈上的前一个ViewController出现。我怎么能用swift完成这个?

答案

一种选择是实现自己的自定义后退按钮。您需要将以下代码添加到viewDidLoad方法:

- (void) viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = newBackButton;
}

- (void) back:(UIBarButtonItem *)sender {
    // Perform your custom actions
    // ...
    // Go back to the previous ViewController
    [self.navigationController popViewControllerAnimated:YES];
}

更新:

这是Swift的版本:

    override func viewDidLoad {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
        self.navigationItem.leftBarButtonItem = newBackButton
    }

    func back(sender: UIBarButtonItem) {
        // Perform your custom actions
        // ...
        // Go back to the previous ViewController
        self.navigationController?.popViewControllerAnimated(true)
    }

更新2:

这是Swift 3的版本:

    override func viewDidLoad {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
        self.navigationItem.leftBarButtonItem = newBackButton
    }

    func back(sender: UIBarButtonItem) {
        // Perform your custom actions
        // ...
        // Go back to the previous ViewController
        _ = navigationController?.popViewController(animated: true)
    }
另一答案

这不像我们的事情那么困难。只需为UIButton创建一个具有清晰背景颜色的框架,为按钮指定操作并将其放在导航栏后退按钮上。最后在使用后取下按钮。

这是使用UIImage而不是UIButton完成的Swift 3示例代码

viewWillDisappear

编写需要执行的代码

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if isMovingFromParentViewController {
            if let viewControllers = self.navigationController?.viewControllers {
                if (viewControllers.count >= 1) {
                    let previousViewController = viewControllers[viewControllers.count-1] as! NameOfDestinationViewController
                    // whatever you want to do
                    previousViewController.callOrModifySomething()
                }
            }
        }
    }

执行操作后删除subView

override func viewDidLoad() {
    super.viewDidLoad()
    let imageView = UIImageView()
    imageView.backgroundColor = UIColor.clear
    imageView.frame = CGRect(x:0,y:0,width:2*(self.navigationController?.navigationBar.bounds.height)!,height:(self.navigationController?.navigationBar.bounds.height)!)
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(back(sender:)))
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGestureRecognizer)
    imageView.tag = 1
    self.navigationController?.navigationBar.addSubview(imageView)
    }
另一答案

斯威夫特3:

func back(sender: UIBarButtonItem) {

    // Perform your custom actions}
    _ = self.navigationController?.popViewController(animated: true)

    }
另一答案

试试这个 。

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    for view in (self.navigationController?.navigationBar.subviews)!{
        if view.tag == 1 {
            view.removeFromSuperview()
        }
    }

试试这个。

override func didMove(toParentViewController parent: UIViewController?) {
    super.didMove(toParentViewController: parent)

    if parent == nil{
        print("Back button was clicked")
    }
}
另一答案

据我所知你想要清空你的self.navigationItem.leftBarButtonItem?.target = "methodname" func methodname ( ) { // enter code here } ,当你按下你的后退按钮并弹出你以前的override func viewWillAppear(animated: Bool) { //empty your array } 你在这个屏幕上加载的array

ViewController let
另一答案
Array
另一答案

我通过调用/重写let settingArray = NSMutableArray() @IBAction func Back(sender: AnyObject) { self. settingArray.removeAllObjects() self.dismissViewControllerAnimated(true, completion: nil) } 然后访问 override public func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.topItem?.title = GlobalVariables.selectedMainIconName let image = UIImage(named: "back-btn") image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(Current[enter image description here][1]ViewController.back) ) } func back() { self.navigationController?.popToViewController( self.navigationController!.viewControllers[ self.navigationController!.viewControllers.count - 2 ], animated: true) } 的堆栈来完成此操作:

viewWillDisappear
另一答案

只需控制+将条形项拖动到func下方。工作就像魅力

navigationController

override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) let stack = self.navigationController?.viewControllers.count if stack >= 2 { // for whatever reason, the last item on the stack is the TaskBuilderViewController (not self), so we only use -1 to access it if let lastitem = self.navigationController?.viewControllers[stack! - 1] as? theViewControllerYoureTryingToAccess { // hand over the data via public property or call a public method of theViewControllerYoureTryingToAccess, like lastitem.emptyArray() lastitem.value = 5 } } }

另一答案

Swift 4.2:

@IBAction func done(sender: AnyObject) {
    if((self.presentingViewController) != nil){
        self.dismiss(animated: false, completion: nil)
        print("done")
    }
}
另一答案

这是最简单的Swift 5解决方案,它不需要您创建自定义后退按钮并放弃您免费获得的所有UINavigationController左按钮功能。

正如Brandon A推荐的那样,您需要在返回之前在要与之交互的视图控制器中实现enter image description here。一种好方法是创建一个可以手动或自动执行的展开segue,并从自定义完成按钮或后退按钮重用相同的代码。

首先,在override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) if self.isMovingFromParent { // Your code... } } 中使您感兴趣的视图控制器(您想要检测到的视图控制器返回)导航控制器的委托:

UINavigationControllerDelegate

其次,在文件底部添加一个覆盖viewDidLoad的扩展名

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.delegate = self
}

由于你的问题包括navigationController(willShow:animated:),我提供了一种方法来获取用户点击的行的索引路径。

另一答案
extension PickerTableViewController: UINavigationControllerDelegate {

    func navigationController(_ navigationController: UINavigationController,
                              willShow viewController: UIViewController,
                              animated: Bool) {

        if let _ = v

以上是关于按下UINavigationController的后退按钮时执行操作的主要内容,如果未能解决你的问题,请参考以下文章

按下UINavigationController的后退按钮时执行操作

按下 UINavigationController 后如何将编辑按钮添加到 UINavigationBar

如何在 UINavigationController 中使用协议和委托

如果用户按下后退按钮,如何调用一些代码

何时取消 uinavigationcontroller 中的 nsurlconnection

UIImagePickerController 和 UINavigationController