2014-02-27 43 views
2

我有一个方法是我正在创建UIView,并且当我从IBAction调用它时它工作正常。但是当我再次调用相同的方法时,它会在顶部绘制另一个视图,并且我认为这是内存泄漏。如何在创建另一个UIView之前删除以前的UIView?谢谢!在创建另一个之前删除以前的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]; 
} 

回答

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; 
} 
+0

工程太棒了!非常感谢!! –

+0

没问题的队友:) – RaffAl

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; 
} 
+0

是的,但我不能在创建新视图之前使用这一行代码,否则它说在对象类型ViewController上找不到Property questionView。 –

+0

@DomasfromUK让它全局声明在.h文件中,像UIView * myview; – morroko

+0

我觉得这个解决方案(稍微)更正确,因为它显示了需要从ViewController引用属性。此外,我可以删除一个UIView子类删除几个子视图。所有这些都来自ViewController,并在2017年与iOS10.3! – Greg

0

使用本

[myview removeFromSuperview]; 
0

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

或者,更好的是保留对它的引用,然后有一个方法来重新载入它的信息并将它动画化/回到视图中(也许通过淡出,重新载入数据,然后将其淡入) 。这样你就不会一直抛弃/重新创建视图,而是重新使用相同的已创建视图。

0
[self.questionView removeFromSuperview]; 

在添加其他视图之前,请将其从超级视图中移除!

0

为什么不重用看法? 如果你宁愿每次都删除它,你不想保持对它的引用,你可以随时设置(脏,但简单):

questionView.tag = SOME_CONST; 

,然后在方法的开头添加:

[[self.view viewWithTag:SOME_CONST] removeFromSuperview]; 
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; 
} 

希望片段和评论里面是有道理的。

快乐编码!

+0

有道理!谢谢! –

相关问题