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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:10:46+00:00 2026-05-23T20:10:46+00:00

I have a view controller that I would like to be able to dismiss

  • 0

I have a view controller that I would like to be able to dismiss with a upper right “done” button on the nav bar. I am not using uinavcontoller, but just added the nav bar like this in my viewDidLoad:

  bar = [[UINavigationBar alloc] init];

in .h:

IBOutlet UINavigationBar *bar;

And of course, connected the nav bar on IB.

Then I tried to add a nav item also within viewDidLoad but nothing happens:

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];

Please note – there are a few questions like that here, but none of them answers this exact question.

thanks for the help!

  • 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-23T20:10:46+00:00Added an answer on May 23, 2026 at 8:10 pm

    You won’t want to alloc/init the bar in your code if you already have it in IB. That will create a second version of it and messages can get skewed. If you are placing the bar into your view in IB, it would be easiest to place your Done button in there as well. First, you’ll want to use UINavigationItem instead of UINavigationBar. Drag a UIBarButtonItem onto the right side of your UINavigationItem, change the text from Item to Done, and select the style. Then you will create an IBAction method to do what you want the button to do (dismiss the view). Then it’s just a matter of hooking up the selector of your button (in IB) to the IBAction method and that will do what you want.

    If you want/need to do this programmatically, you will just use the following.

    rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)];
    navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
    

    You’ll need a dismissView: method, too.

    - (IBAction)dismissView:(id)sender {
        // do something;
    }
    

    EDIT: to do this all programmatically, you’ll start by adding a navigation bar to your header file

    UINavigationBar *navBar;
    

    You’ll only really need to do this if you plan to allow orientation changes. Since you’re not using IB, there’s no need to make this an outlet, so we’re done in the header.

    In the implementation file, you want to call the following:

    - (void)viewDidLoad {
        navBar = [[UINavigationBar alloc] init];
        UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:@"some title"] autorelease];
        UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)] autorelease];
        navItem.rightBarButtonItem = done;
        navBar.items = [NSArray arrayWithObject:navItem];
        [self.view addSubview:navBar];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    }
    
    - (void)dealloc {
        [navBar release];
    }
    

    This starts by creating the instances and placing them in your view. Once the view appears and the frame size is known to the program, it resizes the navigation bar (which in turn resizes its subviews) to its proper size. Then any time the orientation changes, it resizes accordingly. I’ve tested this just to make sure all of it is good info, and it works fine for me, so it should be good for you as well. Hopefully it helps you out

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

Sidebar

Related Questions

Let's say I have a PHP Model-View-Controller framework that maps an address like http://example.com/admin/posts/edit/5
I have a UIScrollView in my project. I have a view controller I would
I would like to be able to decide where a sub view is placed,
I have a view containing a segmented controller and a UIDatePicker that only shows
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when
Lets say I have a view, myView, a view controller, myViewController, and some sort
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
When submitting one form from view, how can i also read or have controller
I have a view that has a list of jobs in it, with data

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.