0
votes

I am bit stack with creating two UIViews which are switchable (each UIView has some of subviews). Since I do not IB, I wanna do that programmatically.

I have tried adding my subviews to my first UIView, then the same with second one and then switching to view like this.

 if ([self.setView superview]) {
  [self.setView removeFromSuperview];
  [self.view addSubview:contentView];
        self.navigationItem.title = @"BaseLine";
 } else {
  [self.contentView removeFromSuperview];
  self.view = setView;
        self.navigationItem.title = @"Settings";
 }

but only thing which works correctly was "title" and nothing appeared on the UIViews. The UIView seems to be OK because when I use

self.view = contentView;
or
self.view = setView;

both showing subviews correctly.

Pls someone kick me to the right direction about this.

tnx

EDIT: Also pointing the solution, perhaps something wrong with my initialization in the loadView

CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    contentView = [[UIView alloc] initWithFrame:screenRect];
    setView = [[UIView alloc] initWithFrame:screenRect];

    self.view = contentView;

I tried to add it first [self. view add..] but the app crashed, so I used this

EDIT2

the root controller is UITableViewController..it is in navigation view and after choosing a cell this UIVieController (percView) with two UIVies is allocated

     ShakeControl *percView = [[ShakeControl alloc] init];
     [self.navigationController pushViewController:percView animated:YES];
     [percView release];

EDIT3 switch is done by button, but it is fine done because I used that many times and it worked

2

2 Answers

3
votes

Here is an example of a view controller that does what you want (I think). It is very basic, just switching between two views with different color backgrounds. But, you could further customize those subviews in the loadView method to display whatever you need.

The interface file:

@interface SwapViewController : UIViewController {
    UIView *firstView;
    UIView *secondView;
    BOOL displayingFirstView;
    UIButton *swapButton;
}
@property (nonatomic, retain) UIView *firstView;
@property (nonatomic, retain) UIView *secondView;
@property (nonatomic, retain) UIButton *swapButton;

- (void)swap;

@end

The implementation file:

#import "SwapViewController.h"

@implementation SwapViewController

@synthesize firstView;
@synthesize secondView;
@synthesize swapButton;

- (void)loadView 
{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];

    UIView *containerView = [[UIView alloc] initWithFrame:frame];
    containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.view = containerView;
    [containerView release];

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

    secondView = [[UIView alloc] initWithFrame:frame];
    secondView.backgroundColor = [UIColor blueColor];

    self.swapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.swapButton.frame = CGRectMake(10, 10, 100, 50);
    [swapButton addTarget:self action:@selector(swap) forControlEvents:UIControlEventTouchUpInside];

    displayingFirstView = YES;
    [self.view addSubview:firstView];
    [self.view addSubview:swapButton];
}

- (void)swap
{
    if(displayingFirstView) {
        [self.firstView removeFromSuperview];
        [self.view addSubview:secondView];

        displayingFirstView = NO;
    } else {
        [self.secondView removeFromSuperview];
        [self.view addSubview:firstView];

        displayingFirstView = YES;
    }
    [self.view bringSubviewToFront:self.swapButton];
}

@end
0
votes

sample code;


BOOL firstview = YES; // First, I have added view1 as a subview;

if (firstview) {
  [view1 removeFromSuperview];
  [self.view addSubview:view2];
 } else {
  [view2 removeFromSuperview];
  self.view addSubview:view1];
 }