自定义视图中的 Crasher 作为 UINavigationControllers navigationItem.titleView
我有一个 UIViewController,其视图属性中有几个子视图(UISearchbar 和几个 UIButton)。 UIButton
连接到典型的 IBAction
,例如 UIControlEventTouchUpInside
的 -(IBAction)buttonPressed:(id)sender
> 状态 - 我是在 IB 中还是以编程方式执行此操作并不重要。
- (void)viewDidLoad {
MUZTitleViewController *title = [[MUZTitleViewController alloc]
initWithNibName:nil bundle:nil];
self.navigationItem.titleView = title.view;
}
在我的项目中还有一个 UINavigationController。当我将 UINavigationBar
的 navigationItem.titleView
设置为我的 UIViewController
视图的视图时,一旦我点击,我就会收到 EXC_BAD_ACCESS 异常按钮之一。我不知道这是为什么。
我上传了一个小示例项目来说明我的问题: Test010.xcodeproj (它启用了 ARC
)结论是使用 UIViewController
的视图并将其分配给 titleView
不是一个好主意,但我在这里没有看到任何替代方案。
编辑:抱歉,示例项目注释掉了导致异常的调用。我重新上传了链接的项目文件。
编辑^2:正如PengOne指出的那样,我已经跳过了收到的确切错误消息:
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'
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;
}
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.
I uploaded a small sample project to illustrate my problem: Test010.xcodeproj (it's ARC enabled)
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.
Edit: Sorry, the sample project commented out the call which causes the exception. I reuploaded the linked project file.
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将 NSZombieEnabled 设置为 YES?如果我这样做,控制台会显示以下输出:
由于项目启用了 ARC,因此控制器似乎在这行之后的某个时间被释放:
我不确定最好的解决方案是什么,但属性肯定有助于防止异常像这样:
Have you tried setting NSZombieEnabled to YES? If I do this, the console shows the following output:
As the project is ARC enabled, the controller seems to get deallocated some time after this line:
I am not sure what the best solution is, but a property definitely helps to prevent the exception like so:
您在 ARC 中遇到的问题也可以通过将应用程序的初始视图控制器设置为主窗口的 rootViewController 属性而不是使用 addSubview 来解决。
这样做可以避免将每个自定义视图控制器添加为属性的需要。
The problem that you were having with ARC can also be resolved by setting the initial view controller of your application as your main window's
rootViewController
property instead of usingaddSubview
.Doing this avoids the need to add each custom view controller as a property.