使用后退按钮展开 segue 时执行代码

Perform code when unwind segue with back button

我想在 segue 后退一步时执行一些代码。例如,当后退按钮被选中时,我想执行一些代码。

我无法从后退按钮创建新的展开操作,因为故事板中没有后退按钮。有没有一种方法可以在选择后退按钮后立即插入代码?

如果您想坚持使用默认的后退按钮,一种方法是子类化导航控制器,并覆盖 popViewControllerAnimated: 当您点击后退按钮时调用它。

-(UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *vcToBePopped =[super popViewControllerAnimated:animated];
    id vc = self.viewControllers.lastObject; // this will be the controller you're going back to
    if ([vc isKindOfClass:IntendedViewController class]) { // replace IntendedViewController with the actual class name you care about
        [(IntendedViewController *)vc someMethod];
    }

    return vcToBePopped;
}