如何从superView中删除View?

时间:2014-05-02 12:49:43

标签: ios objective-c storyboard xib superview

我制作了一个xib并通过以下代码在视图中显示。

    NSArray *xibContents = 
        [[NSBundle mainBundle] loadNibNamed:@"PickupAddressView"
            owner:self 
            options:nil];

    UIView *view = [xibContents objectAtIndex:0]; // Safer than objectAtIndex:0
    //UIView *subView=[view.subviews objectAtIndex:0];

    [view setFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f)];
    CGRect tempFrame=view.frame;
    tempFrame.size.width=300;//change acco. how much you want to expand
    tempFrame.size.height=350;
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.2];
    view.frame=tempFrame;
    //[UIView commitAnimations];
    [view setFrame:CGRectMake(10.0f, 90.0f, 300.0f, 350.0f)];
    [view setBackgroundColor:
      [UIColor colorWithPatternImage:
        [UIImage imageNamed:@"Register_bg"]]];
    [UIView commitAnimations];

    [view removeFromSuperview];
    [self.view addSubview:view];

我正在使用UIStoryboard,并使用xib并显示以下代码。

如何删除此视图。我有UIButton,其IBAction正在被调用。

由于

2 个答案:

答案 0 :(得分:8)

保持对视图的弱引用(属性或实例变量)并像这样调用removeFromSuperview

[self.viewToRemove removeFromSuperview];

答案 1 :(得分:2)

从超级视图中删除任何视图只是输入

太简单了
[yourview removeFromSuperview];

如果您将视图声明为属性,请使用如下所示。

[self.yourview removeFromSuperview];