UIBarButtonItem 使我的 iPhone 应用程序崩溃
我正在开发一个项目,并且正在尝试以编程方式尽我所能。
我必须将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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//
我在使用
addSubview
时也遇到了这个问题,但使用(nonatomic, Strong)
Strong 创建属性为我解决了这个问题。I also had this problem when using
addSubview
but creating a property with(nonatomic, strong)
strong solved it for me.Button 尝试将自身作为参数传递给测试方法。我猜你的该方法的签名不包含参数,因为你的选择器中没有冒号(它应该是
@selector(test:)
)。方法实现应该如下所示: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: