自定义视图中的 Crasher 作为 UINavigationControllers navigationItem.titleView

发布于 2024-12-03 23:32:24 字数 1375 浏览 0 评论 0原文

我有一个 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。当我将 UINavigationBarnavigationItem.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 UIButtons). The UIButtons hooked up to typical IBActions 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 UIViewControllers 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 UIViewControllers 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清君侧 2024-12-10 23:32:24

您是否尝试过将 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;

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

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;
盗琴音 2024-12-10 23:32:24

您在 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 using addSubview.

Doing this avoids the need to add each custom view controller as a property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文