Cocoa-touch中的自动引用计数 - 内联最佳实践

时间:2011-10-17 13:42:08

标签: ios cocoa-touch automatic-ref-counting

在ARC开始进行ios开发之前,我一直在viewDidLoad中使用这样的东西设置我的导航项目:

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)] autorelease];

如果在为视图控制器启用ARC的同时保持一线交易,我该如何正确实现?

我知道在__autoreleasing,__strong等之前添加,但我不知道如何创建这个rightBarButtonItem而不将它分成2行:

__autoreleasing UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)];
self.navigationItem.rightBarButtonItem = rightBarItem;

1 个答案:

答案 0 :(得分:3)

在这种情况下,您不需要使用__autoreleasing限定符。 rightBarButtonItem对指定的对象具有强引用,然后在释放rightBarButtonItem时释放指定的对象(当释放UINavigationBar时)。

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClicked:)];