Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3390884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:44:34+00:00 2026-05-18T03:44:34+00:00

I am bit stack with creating two UIViews which are switchable (each UIView has

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:44:34+00:00Added an answer on May 18, 2026 at 3:44 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of (64-bit) addresses that represent a stack frame, and I
A bit new with WPF...It seems like not matter what I change with horizontal
I just wonder a bit whether or not GDI+ is still a technology worth
A location table record has two address id's - mailing and business addressID that
I am bit confused about ADO.Net Data Services. Is it just meant for creating
Okay, this is a bit abstract, but here goes: I'm creating a website and
I've searched the web for this bit to no avail - I Hope some
Am a bit confused on how stack and heap are arranged in multithreaded processes:
I'm a bit stuck on this. Basically I want to do something like the
I am a bit stuck on the design of my seo friendly urls for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.