0

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

这是我正在实现的代码

-(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];    
}
4

4 回答 4

3

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

在 .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];   

}
于 2013-03-05T06:10:28.123 回答
0
UIView *popoverView;
UIView *popoverViewBackground;

在 .h 文件中声明 popoverView 和 popoverViewBackground 并在 popoverCount 为零时分配和初始化这些子视图。

-(IBAction)showPopover:(id)sender{


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

    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;
        [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];    
}
于 2013-03-05T06:10:53.277 回答
0

您的代码中至少有 2 个问题:

  1. 每次涉及该方法时,您都在创建这些子视图的新实例
  2. 您正在删除新创建的实例。也就是说,之前添加的实例仍然存在。

你应该

  • 将这些子视图保存在实例变量中,以便您可以引用它们并正确删除它们。
  • 将创建代码移到第一个 if 块中,这样您就不会创建太多子视图。
于 2013-03-05T06:17:46.243 回答
0

这是因为您在不同的对象上应用 addSubview 和 removeFromSuperview 。

当第一次调用 showPopover 时,它会创建两个 UIView 对象,
名称为 popoverView 和 popoverViewBackground。
并将其添加到 self.view。

到目前为止,一切都很好,但是当此方法第二次调用
popoverView 和 popoverViewBackground 的新对象时,
您正在尝试从不存在的 self.view 中删除该新创建的对象。

如何解决这个问题:

有两种方法可以解决这个问题:

1) 在 .h 文件中创建此对象,以便您可以从任何地方访问它。

2)以仅在第一次方法调用时创建的方式创建对象。给出这个 UIView 标签,当你删除 UIView.subview 时找到这个标签

方法 1 很简单,方法 2 需要一些代码,但您可以在内存方面提高效率。

祝一切顺利

于 2013-03-05T06:19:18.250 回答