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

  • SEARCH
  • Home
  • 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 695813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:59:53+00:00 2026-05-14T02:59:53+00:00

I am just trying to get my head around simple view switching for the

  • 0

I am just trying to get my head around simple view switching for the iPhone and have created a simple app to try and help me understand it.

I have included the code from my root controller used to switch the views. My app has a single toolbar with three buttons on it each linking to one view. Here is my code to do this but I think there most be a more efficient way to achieve this? Is there a way to find out / remove the current displayed view instead of having to do the if statements to see if either has a superclass?

I know I could use a tab bar to create a similar effect but I am just using this method to help me practice a few of the techniques.

-(IBAction)switchToDataInput:(id)sender{
 if (self.dataInputVC.view.superview == nil) {
  if (dataInputVC == nil) {
   dataInputVC = [[DataInputViewController alloc] initWithNibName:@"DataInput" bundle:nil];
  }
  if (self.UIElementsVC.view.superview != nil) {
   [UIElementsVC.view removeFromSuperview];
  } else if (self.totalsVC.view.superview != nil) {
   [totalsVC.view removeFromSuperview];
  }

  [self.view insertSubview:dataInputVC.view atIndex:0];
 }
}

-(IBAction)switchToUIElements:(id)sender{
 if (self.UIElementsVC.view.superview == nil) {
  if (UIElementsVC == nil) {
   UIElementsVC = [[UIElementsViewController alloc] initWithNibName:@"UIElements" bundle:nil];
  }
  if (self.dataInputVC.view.superview != nil) {
   [dataInputVC.view removeFromSuperview];
  } else if (self.totalsVC.view.superview != nil) {
   [totalsVC.view removeFromSuperview];
  }

  [self.view insertSubview:UIElementsVC.view atIndex:0];
 }

}

-(IBAction)switchToTotals:(id)sender{
 if (self.totalsVC.view.superview == nil) {
  if (totalsVC == nil) {
   totalsVC = [[TotalsViewController alloc] initWithNibName:@"Totals" bundle:nil];
  }
  if (self.dataInputVC.view.superview != nil) {
   [dataInputVC.view removeFromSuperview];
  } else if (self.UIElementsVC.view.superview != nil) {
   [UIElementsVC.view removeFromSuperview];
  }

  [self.view insertSubview:totalsVC.view atIndex:0];
 }
}
  • 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-14T02:59:54+00:00Added an answer on May 14, 2026 at 2:59 am

    I would recommend not re-creating each view every time you want to display, but rather just bringing the correct subview to the front when required. Something like:

    -(void)init{
      // The 3 view controllers below are ivars, so we can access in other methods 
      dataInputVC = [[DataInputViewController alloc] initWithNibName:@"DataInput" bundle:nil];   
      UIElementsVC = [[UIElementsViewController alloc] initWithNibName:@"UIElements" bundle:nil];
      totalsVC = [[TotalsViewController alloc] initWithNibName:@"Totals" bundle:nil];
    
      // Add as subviews (rearrange so that correct view appears first)
      [self.view addSubview:dataInputVC.view];
      [self.view addSubview:UIElementsVC.view];
      [self.view addSubview:totalsVC.view];
    }
    
    -(IBAction)switchToDataInput:(id)sender{
      [self.view bringSubviewToFront:dataInputVC.view];
    }
    
    -(IBAction)switchToUIElements:(id)sender{
      [self.view bringSubviewToFront:UIElementsVC.view];
    }
    
    -(IBAction)switchToTotals:(id)sender{
      [self.view bringSubviewToFront:totalsVC.view];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my head around Objective-C for the iPhone. My app is
I am just trying to get my head around SSL. I have set up
I am trying to get my head around MVVM. For a simple list->data view
Just trying to get my head around what can happen when things go wrong
I am just trying to get my head around various pointer concepts and I
Just trying to still get my head around IOC principles. Q1: Static Methods -
I'm trying to get my head around a query and I just can't figure
I'm trying to get my head around solving the following problem. I have the
Im trying to get my head around MySQL. I have learnt a great deal
Just trying to get my head around Javascript a very strange thing has happened.

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.