سؤال

Does the following code (removeViews) correctly remove the reference to the objects, i.e delete them, so I do not keep making more Views when the method createViews is called. createViews creates the views and removeViews sets them to nil. Note: this is a very simple example to enhance understanding, and serves no actual purpose.

-(void) createViews{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,200,200)];
    tableView.delegate=self;
    tableView.datasource = self;
    self.mainTableView = tableView;//self.mainTableView is a weak reference
    [self.view.superView addSubview: self.mainTableView];

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];
    view.backgroundColor = [UIColor redColor];
    self.mainView = view;//self.mainView is a strong reference

    [self.view.superView addSubviews:self.mainView];

}

-(void) removeViews{
    self.mainView = nil;
    self.mainTableView=nil;



 }
هل كانت مفيدة؟

المحلول

First remove them from their superView and then set them to nil

-(void) removeViews{
        [self.mainView removeFromSuperview];
        [self.mainTableView removeFromSuperview];
        self.mainView = nil;
        self.mainTableView=nil;
     }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top