且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用新的 viewController 替换当前的 viewController

更新时间:2023-12-02 22:39:22

使用category替换控制器:

Use category for controller replace:

//  UINavigationController+ReplaceStack.h


@interface UINavigationController (ReplaceStack)

- (void) replaceLastWith:(UIViewController *) controller;

@end

//  UINavigationController+ReplaceStack.m

#import "UINavigationController+ReplaceStack.h"

@implementation UINavigationController (ReplaceStack)

- (void) replaceLastWith:(UIViewController *) controller {
    NSMutableArray *stackViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [stackViewControllers removeLastObject];
    [stackViewControllers addObject:controller];
    [self setViewControllers:stackViewControllers animated:YES];
}

@end