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 7411513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:23:53+00:00 2026-05-29T06:23:53+00:00

Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created

  • 0

Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created programmatically and all are in separate files:

In AppDelegate.m didFinishLaunchingWithOptions method I have these lines of code

self.rootViewController = [[rootViewController alloc]initWithNibName:@"rootViewController" bundle:nil];
self.window.rootViewController = self.rootViewController;

In rootViewController.m loadview method I have

self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
self.firstView = [[FirstView alloc]init];
[self.view addSubview:self.firstView.view]; 

That code works fine – first view is displayed.

Let’s continue

In FirstView.m switchViews method

NOTE: Please see the comments in code

 self.secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];


// I think here secondView is added to rootViewController - right ?
[self.view.superview addSubview:self.secondView.view];

// Here first view is removed from rootViewController - right ?
[self.view removeFromSuperview];

Here is how I add/remove views.

Is this approach correct?
Can you recommend a better solution?

I have read about UINavigationController, but I don’t think it could be a solution in this case.

  • 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-29T06:23:54+00:00Added an answer on May 29, 2026 at 6:23 am

    You say:

    I have 4 views (call them FirstView …

    Then you say:

    [self.view addSubview:self.firstView.view]; 
    

    Which makes me think that FirstView isn’t actually a UIView – as you claim it is. Instead, it’s probably a UIViewController – a different beast altogether.

    If my suspicion is correct – then you are “off-track” so to speak.

    Going beyond that to your sample code snippet:

    self.secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    
    // I think here secondView is added to rootViewController - right ?
    [self.view.superview addSubview:self.secondView.view];
    
    // Here first view is removed from rootViewController - right ?
    [self.view removeFromSuperview];
    

    This is definitely not a great idea. Here’s why:

    First: your view controller doesn’t explicitly “know” anything about the superview you are so casually inserting and removing subviews to/from – so it shouldn’t do that. You may, alternatively, create your own view and insert/remove subviews from that – which would not only be perfectly acceptable but also common practice.

    Second: if these are actually UIViewControllers like I think they are – then you are not properly handling hooking them up to the UIViewController event chain – which means methods on these subclasses like viewDidAppear: or viewDidUnload will not fire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say i have two view controllers, firstViewController and secondViewController. The firstViewController is supposed
So let's say I have a view that access a date: <%= Html.TextBoxFor(model =>
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Lets say that I have a header user control in a master page, and
Lets say that you have a business object whose current state implies that there
Lets say that I have an executable and when it is started I want
Lets say that I have only DHT (distributed hash table) implemented (in Python), and
Lets say that I have the following code: public class Shelter<A extends Animal, B
To use as an example: lets say that I have 2 EditText s and
Lets just say that I have three textboxes: TextBox1, TextBox2, TextBox3. Normally if I

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.