Vikings
Vikings

Reputation: 2527

iPhone Navigation Back Button

I am having issues with the back button not showing up on the SettingsViewController. The navigation bar does show up when the view is pushed, but no back button.

I am creating this inside a view controller, which is not a navigation controller. Any ideas or suggestions on what is actually going on here.

- (void)viewDidLoad
{
    self.title = @"Settings";
}

- (IBAction)showSettingsModal:(id)sender 
{    
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:settingsViewController] autorelease];

    [self presentModalViewController:navController animated:YES];
    [settingsViewController release];    
}

Upvotes: 0

Views: 3367

Answers (4)

LJ Wilson
LJ Wilson

Reputation: 14427

You are creating a new navigation stack. You will need to add your own Back button and set the action of that to a delegate method on the calling VC to dismiss it.

UPDATE: There seems to be lots of confusion about where and how to dismiss ModalViewControllers. The wrong thing to do in most cases is to call the Dismiss method from the Modal VC itself if you are wanting the parent to act on that dismissal. Instead, use delegation. Here is a simple example:

ModalViewController.h:

@protocol ModalViewControllerDelegate
-(void)dismissMyModalVC;
@end


@interface ModalViewController : UIViewController {
id < ModalViewControllerDelegate > delegate;
}

@property (nonatomic, retain) id < ModalViewControllerDelegate > delegate;
// The rest of your class properties, methods here

ModalViewController.m

@synthesize delegate;

...

// Put in the Method you will be calling from that Back button you created
[delegate dismissMyModalVC];

CallingViewController.h:

#import "ModalViewController.h"

@interface CallingViewController : UIViewController 
<ModalViewControllerDelegate> 
// Rest of class here

CallingViewController.m:

ModalViewController *mvc = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil];
mvc.delegate = self
[self presentModalViewController:mvc animated:YES];

...

// The ModalViewController delegate method
-(void)dismissMyModalVC {
// Dismiss the ModalViewController that we instantiated earlier
[self dismissModalViewControllerAnimated:YES];

That way the VC gets dismissed properly from the controller that instantiated it. That delegate method can be modified to pass along objects as well (like when you are finished logging a user in, etc)

Upvotes: 3

Maggie
Maggie

Reputation: 8081

You are presenting your new controller as modal view controller. Modal controller presents its topmost. You should:

 [self.navigationController pushViewController:navController animated:YES];

to push view controller onto the stack, and then you will see Back button.

Read Apple documenation on presenting view controllers: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

EDIT Didn't see that the calling view controller is not part of the navigation controller. In that case, you will have to create back button manually, and set it as a left bar navigation item.

Upvotes: 0

Janub
Janub

Reputation: 1594

you can try this

UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back"style:UIBarButtonItemStylePlain target:self.navigationItem.backBarButtonItem action:@selector(dismissModalViewControllerAnimated:)];

Upvotes: 1

Philip Sheard
Philip Sheard

Reputation: 5825

SettingsViewController does not have a back button because it is at the bottom of stack. If you want a button to dismiss the modal dialog, you will have to add it yourself.

Upvotes: 1

Related Questions