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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:45:39+00:00 2026-05-28T16:45:39+00:00

I’m study iOS development. Now playing with views. I don’t use interface builder at

  • 0

I’m study iOS development. Now playing with views. I don’t use interface builder at the moment, because I prefer to understand how all stuffs works under the hood. This is why I create my views UI elements etc programmatically. However.
Here is how my project looks like.

I have a class called RootViewController. I use that class as a window rootViewController.
Within that class I have a logic which load/unload views. Here is some code.

RootViewController.m

-(void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.view = view;
    self.view.backgroundColor = [UIColor lightGrayColor];

    // create FirstViewController button
    UIButton *firstViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
             firstViewButton.frame = CGRectMake(20, 60, 280, 50);
             firstViewButton.tag = 1;
             [firstViewButton setTitle:@"Load First view" forState:UIControlStateNormal];
             [firstViewButton addTarget:self 
                   action:@selector(loadViewControlllers:) 
             forControlEvents:UIControlEventTouchUpInside];

    // crate SecondViewController button
    UIButton *secondViewButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
             secondViewButton.frame = CGRectMake(20, 140, 280, 50);
             secondViewButton.tag = 2;
             [secondViewButton setTitle:@"Load Second view" forState:UIControlStateNormal];
             [secondViewButton addTarget:self 
                        action:@selector(loadViewControlllers:) 
              forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:firstViewButton];
    [self.view addSubview:secondViewButton];
    [self.view release];
}

-(void)loadViewControlllers:(UIButton *)sender
{
    if ([sender tag] == 1) {
        if(firstViewController == nil) {
            firstViewController = [[FirstViewController alloc]
                                   initWithNibName:@"FirstViewController" 
                                   bundle:nil];
            [self.view addSubview:firstViewController.view];
        }  
    } else {
        if(secondViewController == nil) {
            secondViewController = [[SecondViewController alloc]
                                    initWithNibName:@"SecondViewController" 
                                   bundle:nil];
            [self.view addSubview:secondViewController.view];
        }
    }
}

The first and the second view controllers contain same code

-(void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];   
     self.view = view;

    self.view.backgroundColor = [UIColor redColor];

    UIButton *remove = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    remove.frame = CGRectMake(20, 60, 280, 50);
    [remove setTitle:@"Remove First view from superview" forState:UIControlStateNormal];
    [remove addTarget:self 
               action:@selector(removeFromView:) 
     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:remove];
    [self.view release];
}

-(IBAction)removeFromView:(id)sender
{
    [self.view removeFromSuperview];
}

So far, so good.
Here is where my questions begin.

1) Is this the correct approach to show/remove views. One controller (rootViewController in my case) to rule them all ?

2) Could you suggest me a way to pass data between first, second and root views ? Lets say a firstViewController is loaded. That controller contains textfield and a button. What I want is when a user type something in a textfield and a button is pressed the data in that textfield to be able to read by rootViewController and secondViewController

3) The applications works well. No crashes etc. The problem is that when neither of first or second controller are called once they can’t be loaded again.
When I press a button nothing is happens.

Last but not lest, sorry about my language.

  • 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-28T16:45:40+00:00Added an answer on May 28, 2026 at 4:45 pm

    1) Is this the correct approach to show/remove views. One controller
    (rootViewController in my case) to rule them all ?

    It’s OK. However, if the firstViewController and the secondViewController share exactly the same logic, then they are actually one class, and it is generally safe for you to remove the redundancy.

    2) Could you suggest me a way to pass data between first, second and
    root views ? Lets say a firstViewController is loaded. That controller
    contains textfield and a button. What I want is when a user type
    something in a textfield and a button is pressed the data in that
    textfield to be able to read by rootViewController and
    secondViewController

    You can use a singleton instance to hold the shared state(, the model in the MVC pattern), but it is not recommended.
    IMHO, you can use an Observer model with Key-value-observing pattern. See Apple’s guide here. Or you can post NSNotification(s) from one controller to notify any other controllers that is interested in that type of notifications. See [guides here].2

    3) The applications works well. No crashes etc. The problem is that
    when neither of first or second controller are called once they can’t
    be loaded again. When I press a button nothing is happens.

    In your code above, the subviews were only added when the controller ivars were nil. Try to fix that logic.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.