我的UIViewController
在其视图属性中有多个子视图(UISearchbar
和几个UIButton
s)。对于UIButton
州,IBAction
与-(IBAction)buttonPressed:(id)sender
的典型UIControlEventTouchUpInside
相似 - 如果我在IB中或以编程方式执行此操作无关紧要。
- (void)viewDidLoad {
MUZTitleViewController *title = [[MUZTitleViewController alloc]
initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
}
在我的项目中还有UINavigationController
。当我将navigationItem.titleView
的{{1}}设置为UINavigationBar
视图的视图时,只要点击其中一个按钮,我就会收到EXC_BAD_ACCESS异常。我不知道为什么会这样。
我上传了一个小示例项目来说明我的问题:Test010.xcodeproj(已启用ARC)
我越来越多地得出结论,使用UIViewController
视图并将其分配给UIViewController
并不是一个好主意,但我在这里看不到任何替代方案。
编辑抱歉,示例项目注释掉了导致异常的调用。我重新上载了链接的项目文件。
编辑^ 2:正如PengOne所指出的那样,我已经跳过了我收到的确切错误消息:
titleView
答案 0 :(得分:3)
您是否尝试过将NSZombieEnabled设置为YES?如果我这样做,控制台会显示以下输出:
2011-09-10 22:56:23.329 Test010[6481:ef03] *** -[MUZTitleViewController
performSelector:withObject:withObject:]: message sent to deallocated
instance 0x7a7ff70
由于项目启用了ARC,控制器似乎在此行之后的一段时间内被取消分配:
MUZTitleViewController *title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
我不确定最佳解决方案是什么,但是属性肯定有助于防止这样的异常:
// MUZDetailViewController.h
@property (strong, nonatomic) MUZTitleViewController *title;
// MUZDetailViewController.m
@synthesize title;
self.title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
答案 1 :(得分:1)
您可以通过将应用程序的初始视图控制器设置为主窗口的rootViewController
属性而不是addSubview
来解决您使用ARC时遇到的问题。
这样做可以避免将每个自定义视图控制器添加为属性。