Ios uibarbuttoneim使我的iPhone应用程序崩溃

Ios uibarbuttoneim使我的iPhone应用程序崩溃,ios,memory,crash,uibarbuttonitem,automatic-ref-counting,Ios,Memory,Crash,Uibarbuttonitem,Automatic Ref Counting,我正在做一个项目,我正试图以编程的方式尽我所能 我必须在应用程序委托中创建的NavigationController导航栏中添加UIBarButtonItem WPViewController *mainVC = [[WPViewController alloc] initWithNibName:@"WPViewController_iPhone" bundle:nil]; UINavigationController *navCon = [[UINavigationController all

我正在做一个项目,我正试图以编程的方式尽我所能

我必须在应用程序委托中创建的NavigationController导航栏中添加UIBarButtonItem

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
  • 以前从未有过类似的错误

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

- (void) test:(id)sender
ARC中的“Message sent to deallocated instance”表示编译器在发送消息之前已标记并释放了您的项

在调试器中设置NSZombieEnabled、MallocStackLogging和guard malloc。然后,当应用程序崩溃时,在控制台中键入以下内容:


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

在使用
addSubview
时,我也遇到了这个问题,但使用
(非原子,强)
strong创建属性为我解决了这个问题。

我刚刚得到了一个exc\u bad\u access messageset环境变量nszombied,启用为YES并进行调试。以下是输出:-[WPViewController性能选择器:withObject:withObject:]:发送到已解除分配的实例0x6B09C40的消息看起来最好关闭ARC:PTry,以便在初始化UINavigationController时调用initWithRootViewController而不是init。
UINavigationController*navCon=[[UINavigationController alloc]initWithRootViewController:mainVC];
。triyng在什么地方创建按钮项?viewDidLoad是正确的位置。我已经尝试了您所说的,问题仍然存在。如果您阅读OP的comments部分中的输出,您会注意到它不是无效的选择器,它是一个释放的实例。对,带有输出的注释不是我在这里发布了这个答案。很抱歉,
[object release]
在启用ARC的情况下无效。即使您尝试使用
@selector(release)
,它也会抛出一个构建时错误。因此,他需要关闭ARC,因为编译器认为他已经处理完该对象。