UIBarButtonItem 使我的 iPhone 应用程序崩溃

发布于 2024-12-17 20:09:24 字数 885 浏览 2 评论 0原文

我正在开发一个项目,并且正在尝试以编程方式尽我所能。

我必须将 UIBarButtonItem 添加到在 App Delegate 中创建的 NavigationController 的导航栏。

WPViewController *mainVC = [[WPViewController alloc] initWithNibName:@"WPViewController_iPhone" bundle:nil];
UINavigationController *navCon = [[UINavigationController alloc] init];        
[navCon pushViewController:mainVC animated:NO];
[self.window addSubview:navCon.view];      

然后,在此处声明的 WPViewController 的实现文件中,我创建并添加 barbuttonitem 作为 VC 的导航项:

UIBarButtonItem *rBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(test)];
self.navigationItem.rightBarButtonItem = rBarButtonItem;

在此之前声明的名为 test 的方法仅记录“test”,但是当我点击应用程序崩溃的按钮。

请帮助我,这个错误让我发疯。

注意:

  • 我在项目中使用 ARC
  • 以前从未遇到过类似的错误

I'm working on a project and I'm trying to do the most I can programmatically.

I've to add an UIBarButtonItem to a NavigationController's nav bar created in the App Delegate.

WPViewController *mainVC = [[WPViewController alloc] initWithNibName:@"WPViewController_iPhone" bundle:nil];
UINavigationController *navCon = [[UINavigationController alloc] init];        
[navCon pushViewController:mainVC animated:NO];
[self.window addSubview:navCon.view];      

Then in the implementation file of the here declared WPViewController I create and add the barbuttonitem as a navigation item of the VC:

UIBarButtonItem *rBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(test)];
self.navigationItem.rightBarButtonItem = rBarButtonItem;

There is a method called test declared before of this that simply log "test", but when I click on the button the app crashes.

Please help me, this bug is driving me crazy.

Notes:

  • I'm using ARC in my project
  • Never had a similar bug before

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

被你宠の有点坏 2024-12-24 20:09:24

ARC 中的“消息发送到已释放的实例”意味着编译器在发送消息之前已标记并释放了您的项目。

在调试器中设置 NSZombieEnabled、MallocStackLogging 并保护 malloc。然后,当您的应用程序崩溃时,在控制台中键入:

(gdb) info malloc-history //崩溃对象的地址,即 0x543216//

" Message sent to deallocated instance" in ARC means the compiler has marked, and released, your item before your message could be sent.

Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the console:

(gdb) info malloc-history //address of crashing object i.e. 0x543216//

胡大本事 2024-12-24 20:09:24

我在使用 addSubview 时也遇到了这个问题,但使用 (nonatomic, Strong) Strong 创建属性为我解决了这个问题。

I also had this problem when using addSubview but creating a property with (nonatomic, strong) strong solved it for me.

滥情空心 2024-12-24 20:09:24

Button 尝试将自身作为参数传递给测试方法。我猜你的该方法的签名不包含参数,因为你的选择器中没有冒号(它应该是 @selector(test:))。方法实现应该如下所示:

- (void) test:(id)sender

The Button tries to pass itself as an argument to the test method. I guess your signature of that method doesn't include an argument, because there's no colon in your selector (it should be @selector(test:)). And the method implementation should look like:

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