在创建另一个之前删除之前的UIView

时间:2014-02-27 18:29:20

标签: ios objective-c uiview

我有一个方法,我正在创建UIView,当我从IBAction调用它时它工作正常。但是,当我再次调用相同的方法时,它会在顶部绘制另一个视图,我相信这是一个内存泄漏。如何在创建另一个之前删除之前的UIView?谢谢!

- (int)showQuestionMethod:(int)number;
{
    UIView *questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
    questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
                                                       blue:0.93 alpha:1.0];

    [self.view addSubview:questionView];
    questionView.tag = questionNumber;


    UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
    questionLabel.text = question1;
    [questionView addSubview:questionLabel];

    CGRect frame = questionView.frame;
    frame.size.height = questionHeight;
    questionView.frame = frame;
    questionView.tag = questionNumber;

    return currentQuestion;
}


- (IBAction)nextQuestion:(id)sender {
    [self showQuestionMethod:questionNumber];
}

7 个答案:

答案 0 :(得分:3)

创建一个允许您引用视图的属性:

@property(nonatomic, strong) UIView *questionView;

然后更改您的方法以删除旧视图并创建一个新视图:

- (int)showQuestionMethod:(int)number;
{
    // Remove the previous view.
    [_questionView removeFromSuperview];

    // Create a new view.
    _questionView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
    _questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
                                                       blue:0.93 alpha:1.0];
    // Add the view.
    [self.view addSubview:_questionView];
    _questionView.tag = questionNumber;

    UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
    questionLabel.text = question1;
    [_questionView addSubview:questionLabel];

    CGRect frame = _questionView.frame;
    frame.size.height = questionHeight;
    _questionView.frame = frame;
    _questionView.tag = questionNumber;

    return currentQuestion;
}

答案 1 :(得分:2)

最简单的方法是保持对您想要删除的视图的引用(私有属性可以很好地工作)。您可以将代码添加到控制器.m文件的顶部:

@interface MyUIViewController ()

    @property (nonatomic, strong) UIView* questionView;

@end

然后按如下方式修改您的方法:

- (int)showQuestionMethod:(int)number;
{
    [self.questionView removeFromSuperview]

    self.questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
    self.questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
                                                   blue:0.93 alpha:1.0];

    [self.view addSubview:self.questionView];
    self.questionView.tag = questionNumber;


    UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
    self.questionLabel.text = question1;
    [self.questionView addSubview:questionLabel];

    CGRect frame = questionView.frame;
    frame.size.height = questionHeight;
    self.questionView.frame = frame;
    self.questionView.tag = questionNumber;

    return currentQuestion;
}

答案 2 :(得分:0)

在创建新视图之前使用它

[myview removeFromSuperview];

答案 3 :(得分:0)

您可以通过在@property (strong, nonatomic) UIView *questionView;声明中添加@interface来保留对视图的引用,然后调用[_questionView removeFromSuperview];将其删除。

或者,更好的是,保留对它的引用,然后有一个方法来重新加载它上面的信息并将其动画显示/返回到视图中(可能通过淡出,重新加载数据,然后将其淡入)。这样你就不会一直丢弃/重新创建视图,相反,你只是重复使用相同的,已创建的视图。

答案 4 :(得分:0)

[self.questionView removeFromSuperview];

在添加其他视图之前,请从superview中删除它!

答案 5 :(得分:0)

为什么不重用视图? 如果你每次都想删除它而你不想保留它的引用,你可以随时设置(脏但很简单):

questionView.tag = SOME_CONST;

然后在方法的开头添加:

[[self.view viewWithTag:SOME_CONST] removeFromSuperview];

答案 6 :(得分:0)

我知道这个问题已经得到解答,但我想在删除UIView或其他任何你应该FIRST CHECK IF IT IS ALLOCATED之前添加一个预防措施。所以删除应该是:

// Make property first 
@property (nonatomic, strong) UIView *questionView;


- (int)showQuestionMethod:(int)number;
{
    // Check it it is allocated
    if(questionView)
    {
         [questionView removeFromSuperView];
         //[questionView release]; // Un-Comment it if NOT using ARC
         questionView = nil;
    }

    // At this point you safe to create a new UIView.
    questionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 250)];
    questionView.backgroundColor = [[UIColor alloc] initWithRed:0.93 green:0.93
                                                       blue:0.93 alpha:1.0];

    [self.view addSubview:questionView];
    questionView.tag = questionNumber;


    UILabel *questionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
    questionLabel.text = question1;
    [questionView addSubview:questionLabel];

    CGRect frame = questionView.frame;
    frame.size.height = questionHeight;
    questionView.frame = frame;
    questionView.tag = questionNumber;

    return currentQuestion;
}

希望内部的片段和评论是有道理的。

快乐编码!!!