如何用新的 viewController 替换当前的 viewController [英] How to replace current viewController with a new viewController

查看:26
本文介绍了如何用新的 viewController 替换当前的 viewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用新的 viewController 替换当前的 viewController.我以前能够做到这一点,但我遇到了 BAD_ACCESS 的一些问题.

I'm trying to replace my current viewController with a new one. I've been able to do this before but I'm having some issues with BAD_ACCESS.

这是当我想用新视图替换当前视图时将运行的代码.

This is the code that will run when I want to replace the current view with a new one.

(将使用本地属性self.some_data"(非原子,保留)调用该函数)

(The function will be called using a local property "self.some_data" (nonatomic, retain))

-(void) labelSelected:(SomeDataObject*) some_data{ 
   SomeViewController *viewController = (SomeViewController*)[[ClassManager sharedInstance] viewControllerForClassIdentifier:@"com.somename" fromPlistFileName:@"iPhoneScreenList"];


   viewController.data = (NSObject*)some_data;
   [some_data retain];

   //[self.navigationController pushViewController:viewController animated:YES];

   UINavigationController *tempNavigationController = self.navigationController;

   [[self retain] autorelease];

   [tempNavigationController popViewControllerAnimated:FALSE];
   [tempNavigationController pushViewController:viewController animated:TRUE];
}

这里一切正常.问题是如果我发布新的viewController"它会崩溃.如果我选择:

Here everything works fine. The issue is that if I release the new "viewController" it crashes. And if I choose:

[tempNavigationController popViewControllerAnimated:TRUE];

我得到了一些非常奇怪的行为,控制器永远不会被替换,我返回到 rootController 并且导航栏上有两层文本.

I get some really wierd behaviour where the controller never gets replace and I return to the rootController and the navigation bar has two layers of text on it.

如果我这样做:

[tempNavigationController pushViewController:viewController animated:FALSE];

我得到 BAD_ACCESS 并且应用程序崩溃.它以前有效,但现在无效.

I get BAD_ACCESS and the application chrashes. It worked before but not anymore.

我做错了什么?

谢谢!

推荐答案

使用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

这篇关于如何用新的 viewController 替换当前的 viewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆