ios- is "swapping" UIViews possible?

Here's my code:

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 is a simple view embedded into the view this class controls (first initiated by the Nib file), the class method of COLGenerales returns an array of CGRects coded as NSValues, and registrar: is a local method.

Everything gets properly set (I've thoroughly checked this with the debugger). The view gets successfully created, set, and added to the dictionary.

However, I absolutely never get the actual screen to change. I even included the background color change just to check if it isn't some kind of problem with the buttons. Nothing. Any suggested solution to this?

Upvotes: 2

Views: 1468

Answers (2)

Seamus Campbell
Seamus Campbell

Reputation: 17916

A property that is an IBOutlet does not have an intrinsic connection to the view hierarchy—it only makes it possible to populate that property from a xib. When you set self.Botones, you'll need to do something like the following:

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

If you update self.Botones in many places, and you always want the change reflected on-screen, you could add this into a setter implementation:

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

Upvotes: 1

waylonion
waylonion

Reputation: 6976

I recommend using a UINavigation controller that houses these two views.

You can reference this link Swapping between UIViews in one UIViewController

Basically, you create one view, removeSubview for the first and then add the second one with addSubview! [view1 removeFromSuperview];

[self.view addSubview: view2];

Other reference sources: An easy, clean way to switch/swap views?

How to animate View swap on simple View iPhone App?

Hopefully this helps!

Upvotes: 1

Related Questions