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

查看:175
本文介绍了如何用新的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.

我做错了什么?

谢谢!

推荐答案

使用类别进行控制器替换:

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天全站免登陆