【问题标题】:Removing subviews when bar button is tapped点击栏按钮时删除子视图
【发布时间】:2013-03-05 17:00:36
【问题描述】:

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

这是我正在实现的代码

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

【问题讨论】:

    标签: ios


    【解决方案1】:

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

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

    【讨论】:

    • 最后一件事可以麻烦您吗?有没有办法将滚动和滚动动画附加到我的popoverview?就像 google plus 在他们的应用中的表现一样?
    • 我现在没有该代码,但我会尽快发布它。
    【解决方案2】:
    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];    
    }
    

    【讨论】:

    • 嗨,我正在使用此代码,但出现错误“在前向类对象'CALayer *'中找不到属性'cornerRadius'”任何解决方案
    • @Dipendra:使用 QuartzCore 框架并在 .h 文件中写入 #include 这一行。
    【解决方案3】:

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

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

    你应该

    • 将这些子视图保存在实例变量中,以便您可以引用它们并正确删除它们。
    • 将创建代码移到第一个 if 块中,这样就不会创建太多子视图。

    【讨论】:

      【解决方案4】:

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

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

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

      如何解决:

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

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

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

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

      一切顺利

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 2020-01-13
        相关资源
        最近更新 更多