简体   繁体   中英

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];    
}

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)

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.

你可以试试这个

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM