iPhone Navigation Back Button

若如初见. 提交于 2019-12-08 13:15:08

问题


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

回答1:


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)




回答2:


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.




回答3:


you can try this

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



回答4:


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.



来源:https://stackoverflow.com/questions/9483100/iphone-navigation-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!