问题描述
我有一个 UIViewController
在它的视图属性中有几个子视图(UISearchbar
和几个 UIButton
s).UIButton
与典型的 IBAction
挂钩,例如 -(IBAction)buttonPressed:(id)sender
用于 UIControlEventTouchUpInside
> 状态 - 我是在 IB 中还是以编程方式进行都没有关系.
I have an UIViewController
with several subviews in its view property (UISearchbar
and several UIButton
s). The UIButton
s hooked up to typical IBAction
s like -(IBAction)buttonPressed:(id)sender
for the UIControlEventTouchUpInside
state - it doesn't matter if I do it in IB or programmatically.
- (void)viewDidLoad {
MUZTitleViewController *title = [[MUZTitleViewController alloc]
initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
}
在我的项目中还有一个 UINavigationController
.当我将 UINavigationBar
的 navigationItem.titleView
设置为我的 UIViewController
视图的视图时,我一点击就收到 EXC_BAD_ACCESS 异常按钮之一.我不知道这是为什么.
In my project there's also an UINavigationController
. When I set the navigationItem.titleView
of the UINavigationBar
to the view of my UIViewController
s view I get an EXC_BAD_ACCESS exception, as soon as I tap one of the button. I don't know why this is.
我上传了一个小示例项目来说明我的问题:Test010.xcodeproj(启用了 ARC)
I uploaded a small sample project to illustrate my problem: Test010.xcodeproj (it's ARC enabled)
我越来越得出结论,使用 UIViewController
的视图并将其分配给 titleView
并不是一个好主意,但我没有看到任何在这里替代.
More and more I come to the conclusion that it's not a good idea to use the UIViewController
s view and assign it to the titleView
but I don't see any alternative here.
抱歉,示例项目注释掉了导致异常的调用.我重新上传了链接的项目文件.
Sorry, the sample project commented out the call which causes the exception. I reuploaded the linked project file.
编辑^2:正如 PengOne 指出的那样,我跳过了我收到的确切错误消息:
Edit^2: As PengOne pointed out I've skipped the exact error message I got:
2011-09-10 23:09:50.621 Test010[78639:f803] -[CALayer buttonPressed:]: unrecognized selector sent to instance 0x9254ae0
2011-09-10 23:09:50.623 Test010[78639:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayer buttonPressed:]: unrecognized selector sent to instance 0x9254ae0'
推荐答案
您是否尝试将 NSZombieEnabled 设置为 YES?如果我这样做,控制台会显示以下输出:
Have you tried setting NSZombieEnabled to YES? If I do this, the console shows the following output:
2011-09-10 22:56:23.329 Test010[6481:ef03] *** -[MUZTitleViewController
performSelector:withObject:withObject:]: message sent to deallocated
instance 0x7a7ff70
由于项目启用了 ARC,控制器似乎在此行之后的某个时间被释放:
As the project is ARC enabled, the controller seems to get deallocated some time after this line:
MUZTitleViewController *title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
我不确定最好的解决方案是什么,但一个属性肯定有助于防止像这样的异常:
I am not sure what the best solution is, but a property definitely helps to prevent the exception like so:
// MUZDetailViewController.h
@property (strong, nonatomic) MUZTitleViewController *title;
// MUZDetailViewController.m
@synthesize title;
self.title = [[MUZTitleViewController alloc] initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
这篇关于自定义视图中的 Crasher 作为 UINavigationControllers navigationItem.titleView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!