gpt4 book ai didi

ios - 点击条形按钮时删除 subview

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

我试图在点击条形按钮时触发添加两个 subview 。但是添加 subview 工作得很好,但是当我尝试删除 subview 时,它不起作用。

这是我正在实现的代码

-(IBAction)showPopover:(id)sender{

UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

UIView *popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1000)];
popoverView.alpha = 0.0;
popoverView.layer.cornerRadius = 2;
popoverView.layer.borderWidth = 0.1f;
popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor;
popoverView.layer.masksToBounds = YES;

popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor;
if (popoverCount == 0) {
[self.view addSubview:popoverViewBackground];
[self.view addSubview:popoverView];
popoverCount = 1;
}else if (popoverCount ==1){
[popoverView removeFromSuperview];
[popoverViewBackground removeFromSuperview];
popoverCount = 0;
}
[popoverViewBackground setAlpha:0.5];
[popoverView setAlpha:1.0];
}

最佳答案

问题是您每次单击按钮时都在创建新 View ,所以旧 View 不会删除像这样放置您的代码,然后它会正常工作。我已经测试过了。

在.h文件中

@interface secondViewController : UIViewController
{

int popoverCount;

UIView *popoverView ;

UIView *popoverViewBackground;
}

在 .m 文件中

- (void)viewDidLoad
{
[super viewDidLoad];


popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)];

popoverViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, -100, 320, 100)];
popoverView.alpha = 0.0;
popoverView.layer.cornerRadius = 2;
popoverView.layer.borderWidth = 0.1f;
popoverView.layer.backgroundColor = [UIColor whiteColor].CGColor;
popoverView.layer.masksToBounds = YES;

popoverViewBackground.layer.backgroundColor= [UIColor blackColor].CGColor;
}

-(IBAction)showPopover:(id)sender {

if (popoverCount == 0) {
[self.view addSubview:popoverViewBackground];

[self.view addSubview:popoverView];

[UIView animateWithDuration:0.5
animations:^{
popoverView.frame = CGRectMake(0,0,320,100);
}
completion:^(BOOL finished){
;
}];
[UIView animateWithDuration:0.5
animations:^{
popoverViewBackground.frame = CGRectMake(0,0,320,100);
}
completion:^(BOOL finished){
;
}];
popoverCount = 1;
}else if (popoverCount ==1){


[UIView animateWithDuration:0.5
animations:^{
popoverView.frame = CGRectMake(0,-100,320,100);
}
completion:^(BOOL finished){
[popoverView removeFromSuperview];
}];
[UIView animateWithDuration:0.5
animations:^{
popoverViewBackground.frame = CGRectMake(0,-100,320,100);
}
completion:^(BOOL finished){
[popoverViewBackground removeFromSuperview];
}];


popoverCount = 0;
}
[popoverViewBackground setAlpha:0.5];
[popoverView setAlpha:1.0];

}

关于ios - 点击条形按钮时删除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216869/

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