ios-“交换”UIViews可能吗?

时间:2013-04-03 19:30:36

标签: ios views

这是我的代码:

if([pantallas objectForKey:par]){
    UIView *vista= [[UIView alloc] initWithFrame:self.Botones.frame];
    vista.backgroundColor= [UIColor brownColor];

    CGSize la=  CGSizeMake(50,60);
    int cuantos= [part2 count];
    NSArray *arr= [COLGenerales tileN:cuantos RectsOfSize:la  intoSpaceOf:vista.frame withMaxPerRow:5 spaceVertical:10 spaceHorizontal:10];
    for(int j=0; j<cuantos; j++){
        UIButton *bot= [[UIButton alloc] initWithFrame:[[arr objectAtIndex:j] CGRectValue]];
        bot.tag=j;
        bot.titleLabel.text=par;
        bot.titleLabel.hidden=true;
        bot.imageView.image = [UIImage imageNamed:[[part2 allKeys] objectAtIndex:j]];
        [bot addTarget:self action:@selector(registrar:) forControlEvents:UIControlEventTouchDown];
        [vista addSubview:bot];

    }

    [pantallas setObject:vista forKey:par];

   self.Botones= vista; 
    }else{
    self.Botones= [pantallas objectForKey:par];
}

Botones是一个嵌入到这个类控件的视图中的简单视图(首先由Nib文件启动),COLGenerales的类方法返回一个编码为NSValues的CGRect数组,而registrar:是一个本地方法。

所有内容都已正确设置(我已使用调试器彻底检查过)。视图成功创建,设置并添加到字典中。

但是,我绝对不会改变实际的屏幕。我甚至包括背景颜色更改只是为了检查按钮是否有某种问题。没有。任何建议的解决方案?

2 个答案:

答案 0 :(得分:1)

我建议使用包含这两个视图的UINavigation控制器。

您可以参考此链接Swapping between UIViews in one UIViewController

基本上,你创建一个视图,删除第一个视图,然后使用addSubview添加第二个视图! [view1 removeFromSuperview];

[self.view addSubview:view2];

其他参考资料来源: An easy, clean way to switch/swap views?

How to animate View swap on simple View iPhone App?

希望这有帮助!

答案 1 :(得分:1)

作为IBOutlet的属性没有与视图层次结构的内在连接 - 它只能从xib填充该属性。设置self.Botones时,您需要执行以下操作:

[self.Botones removeFromSuperview];
self.Botones = newValue;
[self.BotonesSuperview addSubview:self.Botones];

如果您在许多地方更新self.Botones,并且您始终希望在屏幕上反映更改,则可以将其添加到setter实现中:

-(void)setBotones:(UIView*)newValue {
    if (newValue != _Botones) {
        [_Botones removeFromSuperview];
        _Botones = newValue;
        [self.BotonesSuperview addSubview:_Botones];
    }
}