在iOS中的运行时更改子视图

在iOS中的运行时更改子视图,ios,objective-c,uiview,Ios,Objective C,Uiview,在初始化视图控制器时,我声明了一个视图,并将其设置为主视图的子视图: self.customView = [[UIView alloc] initWithFrame:self.view.frame]; self.customView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.customView]; 稍后,在一个方法中,我需要用另一个视图替换self.customView。(注意:更改背景颜色只是一个简单

在初始化视图控制器时,我声明了一个视图,并将其设置为主视图的子视图:

self.customView = [[UIView alloc] initWithFrame:self.view.frame];
self.customView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.customView];
稍后,在一个方法中,我需要用另一个视图替换
self.customView
。(注意:更改背景颜色只是一个简单的示例。视图比这更复杂)

但这没有效果。但是,如果我改为制作如下内容:

[self.view addSubview:anotherView];
它很好用。但是,我希望在不显式地本地化和删除视图的情况下,去掉前面的视图。是否可以在运行时替换子视图,或者我遗漏了什么


我在ARC工作。谢谢

您需要删除旧视图并添加新视图。大概是这样的:

// Remove old customView
[self.customView removeFromSuperview];

// Add new customView
UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];
anotherView.backgroundColor = [UIColor blackColor];
self.customView = anotherView;
[self.view addSubview:self.customView];
编辑:

我甚至没有注意到你所做的只是改变背景颜色。在这种情况下,根本不需要替换视图。只需更新背景颜色:

self.customView.backgroundColor = [UIColor blackColor];

您可以更改旧视图的颜色,而不是删除旧视图并添加新视图,这将比创建/删除视图更优化

您可以通过以下代码更改已添加的视图的颜色

self.customView.backgroundColor=[UIColor blackColor]


您共享的示例代码我觉得这不是正确的编码方式。遵循示例代码

初始化视图并设置隐藏属性

self.customView = [[UIView alloc] initWithFrame:self.view.frame];

self.customView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.customView];

self.customView.hidden = YES;

UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];

anotherView.backgroundColor = [UIColor blackColor];

[self.view addSubview:anotherView];

anotherView.hidden = YES;
在运行时,按选择所需的视图

self.customView.hidden = NO;

anotherView.hidden = YES;
如果您需要self.customView来显示


如果您需要另一个视图来显示

我想对您来说最好的解决方案是在@property customView中编写一个自定义setter:

在标题中:

@property (nonatomic, strong) UIView* customView;
在执行中:

@synthesize customView = _customView;
...
-(void) setCustomView:(UIView *)customView {
    NSUInteger z = NSNotFound;
    if (_customView) {
        z = [self.view.subviews indexOfObject:_customView];
    }
    if (z == NSNotFound) {
        // old view was not in hierarchy
        // you can insert subview at any index and any view you want by default
        [self.view addSubview:customView];
    } else {
        // you can save superview
        UIVIew *superview = _customView.superview;
        [_customView removeFromSuperview];
        //also you can copy some attributes of old view:
        //customView.center = _customView.center
        [superview insertSubview:customView atIndex:z];
    }
    // and save ivar
    _customView = customView;
}
因此,您不需要将customView添加为子视图。
此外,您还可以随时替换您的customView

显然,这是(唯一)可行的方法,但是有问题的开发人员希望避免出于某种原因调用
removeFromSuperview
。@TimvanElsloo我不认为OP希望避免调用
removeFromSuperview
。我认为OP希望简单地重新分配
self.customView
也能神奇地替换屏幕上的视图。为什么不明确地查找并删除视图?您是否担心性能问题,或者您是否有任何其他顾虑?我不想删除它,因为它看起来更干净,而且因为我认为它会因为视图层次结构的搜索步骤而变得更慢(目前相当复杂)。我很好奇为什么不能直接替换视图。也许你的另一个视图被定位在你的customView的边界之外?您可以尝试在initWithFrame中使用customView.bounds而不是self.view.frame:假设实际需要的不仅仅是更改背景颜色,这是一个很好的答案。谢谢,我最终实现了这一点。尽管我仍然很好奇为什么不能直接替换视图。当您尝试替换视图时,旧视图从视图层次结构中删除,新视图只保存在PropertyTanks中,但我没有很好地表达我对颜色的看法。它只是假装一个更复杂的不同观点的简化。
@property (nonatomic, strong) UIView* customView;
@synthesize customView = _customView;
...
-(void) setCustomView:(UIView *)customView {
    NSUInteger z = NSNotFound;
    if (_customView) {
        z = [self.view.subviews indexOfObject:_customView];
    }
    if (z == NSNotFound) {
        // old view was not in hierarchy
        // you can insert subview at any index and any view you want by default
        [self.view addSubview:customView];
    } else {
        // you can save superview
        UIVIew *superview = _customView.superview;
        [_customView removeFromSuperview];
        //also you can copy some attributes of old view:
        //customView.center = _customView.center
        [superview insertSubview:customView atIndex:z];
    }
    // and save ivar
    _customView = customView;
}