gpt4 book ai didi

ios - 从 View 中移除和释放类型对象

转载 作者:行者123 更新时间:2023-11-28 20:26:08 25 4
gpt4 key购买 nike

我有一个名为 Post 的类及其一个 View ....我在我的主视图中多次添加它,如下所示:锁定如何从我的 self.view 中删除和释放此对象....我只有这门课:

@interface Post : UIView{
UILabel * label1;
UILabel * label2;
UILabel * label3;
}
@implementation Post
-(void)init
{
self = [super init];
if (self) {
label1 = [[UILabel alloc]init];
label2 = [[UILabel alloc]init];
label3 = [[UILabel alloc]init];
label1.frame = CGRect(10,10,100,30);
label2.frame = CGRect(10,60,100,30);
label3.frame = CGRect(10,110,100,30);

}
return self;
}
@end

和这个主类 Controller

@implementation HomeViewController
-(void)viewDidLoad{
Post *p = [[Post alloc]init]
p.frame = CGRect(0,0,0,320,460);
p.backgroundColor = [UIColor blue];
[self.view addsubview: p];
[p release];


Post *p2 = [[Post alloc]init]
p2.frame = CGRect(0,0,0,320,460);
p2.backgroundColor = [UIColor blue];
[self.view addsubview:p2];
[p2 release];

Post *p3 = [[Post alloc]init]
p3.frame = CGRect(0,0,0,320,460);
p3.backgroundColor = [UIColor blue];
[self.view addsubview:p3];
[p3 release];

}
-(void)RemoveAllPosts
{
//How to remove and release all post from self.view???
}
@end

最佳答案

请尝试以下代码行。

for(UIView *viewInner in self.view.subviews) {
if([viewInner isKindOfClass:[Post class]])
[viewInner removeFromSuperview];
}

关于ios - 从 View 中移除和释放类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com