最近不得不在离开相当多后再次拿起iOS开发地幔,并且我再次被击中了我的保留/强烈记忆问题。基本上,我有两个视图控制器,我在使用当前视图控制器之间切换。在他们的ViewDidload方法中,我设置了工具栏属性。现在,我不想在几个地方写下这一点,所以我想为什么不在我使用某些应用程序宽的数据创建的Singleton中写这件事。我将其声明为这样的财产:
@property (nonatomic, strong, readonly) NSArray *toolbarItems;
然后我在该类的初始方法中初始化了它:
_toolbarItems = @[
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(menuOne)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(menuTwo)]
];
对于每个视图控制器的ViewDidload方法,我设置了工具栏属性:
self.toolbarItems = [MySingleton shared].toolbarItems;
工具栏项目出现在View Controller One的初始介绍中。当显示视图控制器两个时,也会出现它,但是当我切换回视图控制器时,工具栏项目不再存在。但是,工具栏属性仍表示其中有两个项目。我的猜测是uibarbuttonitem本身已经以某种方式被划分了,但我不知道为什么。
我缺少什么?
我认为您的单身对象被保留,而不是阵列本身在单胎对象中。
使用@[]
自动释放创建的数组对象本身,但不会保留,并通过使用_toolbarItems
将其直接分配给实例变量。我认为您需要做self.toolbarItems = @[
...