从VC A推送UIViewController B,但一旦VC B可见,则释放VC A.

时间:2014-03-01 12:00:32

标签: ios iphone

UIViewControllers总是被推到堆栈层次结构上,无论它是模态存在还是iPhone上的UINavigationController推送。如何在不必保持层次结构和能够使用转换委托的情况下,从另一个呈现UIViewController。

VC = UIViewController

VC A on screen
VC A --present--> VC B
VC B on screen
VC A dealloc
VC B --present--> VC C
VC C on screen
VC B dealloc
VC C --present--> VC X (or whatever other)
VC X on screen
VC C dealloc

etc...

2 个答案:

答案 0 :(得分:0)

我只是想说:不要那样做!这就是它应该添加的方式,就像你完成VC B一样(当用户按下上一个按钮时)你希望VC A恢复原状......

尽管如此,如果出于一个很好的理由,你想要在解释时进行处理,我建议有一个VC Main来管理所有其他的:

VC Main : push VC A
VC A    : Request (delegate) VC Main to show VC B
VC Main : ok, pop VC A + push VC B
VC B    : Request VC Main to show VC X
VC Main : ok, pop VC B + push VC X
VC X    : Request VC main to show VC A
VC Main : ok pop VC X + push VC A (VC A is display as default and not as it was on line 2)

答案 1 :(得分:0)

最佳方法是使用UINavigationController委托。例如,您可以创建自己的UINavigationController子项。

MyNavigationViewController.h文件:

@interface MyNavigationViewController : UINavigationController
@end

MyNavigationViewController.m文件:

@interface MyNavigationViewController () <UINavigationControllerDelegate>
@end

@implementation MyNavigationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 1) {
        self.viewControllers = @[self.topViewController];
    }
}

@end

然后在代码中使用MyNavigationViewController代替UINavigationController