作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试用一个新的替换当前的 viewController。我以前能够做到这一点,但我遇到了 BAD_ACCESS 的一些问题。
这是当我想用新 View 替换当前 View 时将运行的代码。
(该函数将使用本地属性“self.some_data”(非原子,保留)调用)
-(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”,它就会崩溃。如果我选择:
[tempNavigationController popViewControllerAnimated:TRUE];
我遇到了一些非常奇怪的行为, Controller 永远不会被替换,我返回到 rootController,导航栏上有两层文本。
如果我这样做:
[tempNavigationController pushViewController:viewController animated:FALSE];
我收到 BAD_ACCESS 并且应用程序崩溃了。它以前有效,但现在无效了。
我做错了什么?
谢谢!
最佳答案
Controller 替换使用类别:
// 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
关于objective-c - 如何用新的 viewController 替换当前的 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034740/
我是一名优秀的程序员,十分优秀!