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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:59:00+00:00 2026-05-24T03:59:00+00:00

I want to push root view controller. Why doesn’t this code work? RootController *rootController

  • 0

I want to push root view controller. Why doesn’t this code work?

RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];               
[self.navigationController pushViewController:rootController animated:YES];
[rootController release];

I used addSubview like this before.

- (void)cancel {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad { // this is root view controller
    [super viewDidLoad];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];     
}

RootController *rootController = [[RootController alloc]initWithStyle:UITableViewStylePlain];               
UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootController];
self.naviController = aNavigationController;
[aNavigationController release];
[rootController release];
[self.view addSubview:[naviController view]];

And I added cancel button in navigation bar to go back to previous view. It doesn’t work.
So, I want to push instead of add.

  • 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-24T03:59:00+00:00Added an answer on May 24, 2026 at 3:59 am

    You set the root controller in a UINavigationController using the

    initWithRootViewController:
    

    method. So, the way you are doing is correct. I would suggest you to inspect self.view and ensure that it is not nil.

    EDIT: after your comment

    You need to define a root view controller for your UINavigationController to work properly; from the UINavigationController reference:

    Every navigation stack must have at least one view controller to act as the root.

    So you cannot remove the root view controller. Possibly, to make things work as you like you should create an additional view controller to use as root view controller that you do not alter, then push your RootViewController on the navigation stack, then popping would work:

    UIViewController *baseController = [[UIViewController alloc] initWithStyle:UITableViewStylePlain];               
    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:baseController];
    self.naviController = aNavigationController;
    [aNavigationController release];
    [baseController release];
    [self.view addSubview:[naviController view]];
    
    RootController *rootController = [[RootController alloc] initWithStyle:UITableViewStylePlain];               
    [self.navigationController pushViewController:rootController animated:YES];
    [rootController release];
    

    Notice that I first defined a simple UIViewController as root view controller, then pushed your controller on to it.

    Once you do this, if you add the cancel button like you do, it will work popping the rootViewController from the navigation stack.

    AFTER LAST COMMENT:

    If I understand you right, when clicking on the cancel button, you want to get rid of the UINavigationController altogether.

    In this case, use the following code for cancel:

    - (void)cancel {
       [self.navigationController.view removeFromSuperview];
    }
    

    If this guess is right, keep in mind that since you are not keeping any reference to the navigation controller, it will be deallocated and with it all the view controllers you instantiated.

    If instead of removing the UINavigationController altogether, you would simpy hide the navigation bar, then after popping rootController, call:

    setNavigationBarHidden:animated
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UINavigationController with a root view controller and then I push a
So, I push a view controller from RootViewController like: [self.navigationController pushViewController:anotherViewController animated:YES] ; BUT,
My application has a navigation view controller with 2 views : - a root
I have an app with a navigation controller as the root view. There are
I have a view controller that i'm trying to push onto the navigation stack.
I have a simple view based application. I want to switch this original view
I have a project that I want to push to a Mercurial repository on
Read about Server push here . I want to push data to client from
Howday, I want to push data to flex clients. I am talking about anywhere
I created a branch on my local Mercurial repository. I want to push to

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.