这段代码是否有可能在内存方面造成任何问题?

时间:2011-05-11 15:50:04

标签: iphone ios memory-management

我想我隐藏了工具栏。我是否需要进行任何其他取消分配?

 - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];


        self.navigationItem.title = @"Add Recipients";

        self.navigationController.toolbarHidden=NO;


        UIBarButtonItem            *localItem;
        UIBarButtonItem            *remoteItem;


        localItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Local"
                                                       style: UIBarButtonItemStyleBordered
                                                      target: self
                                                      action: @selector( localRecipients: ) ];



        remoteItem = [[ UIBarButtonItem alloc ] initWithTitle: @"Remote"
                                                        style: UIBarButtonItemStyleBordered
                                                       target: self
                                                       action: @selector( remoteRecipients: ) ];


        self.toolbarItems = [ NSArray arrayWithObjects: localItem,remoteItem,nil ];

        [localItem release];
        [remoteItem release];


    }


    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];

        self.navigationController.toolbarHidden=YES;



}

2 个答案:

答案 0 :(得分:2)

您负责释放您创建的对象。由于您没有创建工具栏,因此您不负责发布它。

答案 1 :(得分:2)

您不需要进行取消分配,因为您已经通过释放UIBarButtonItem来解决代码问题。

如果您保留toolbarItems,请尝试使用以下代码。

self.toolbarItems = nil ;
self.toolbarItems = [ NSArray arrayWithObjects: localItem,remoteItem,nil ];