[英]iPhone Navigation Back Button
我遇到后退按钮未显示在SettingsViewController上的问题。 按下视图时,导航栏会显示,但没有后退按钮。
我在视图控制器(不是导航控制器)中创建此控件。 关于这里实际发生的任何想法或建议。
- (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];
}
您正在创建一个新的导航堆栈。 您将需要添加自己的“后退”按钮,并将其操作设置为调用VC上的委托方法以将其关闭。
更新:关于在哪里以及如何关闭ModalViewControllers似乎有很多困惑。 在大多数情况下,错误的做法是,如果您希望父级对该解雇采取行动,则从Modal VC本身调用Dismiss方法。 而是使用委派。 这是一个简单的示例:
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];
这样,VC会从实例化它的控制器中正确退出。 可以将该委托方法修改为也传递对象(例如,完成用户登录后等)
SettingsViewController没有后退按钮,因为它位于堆栈的底部。 如果您想要一个按钮来关闭模式对话框,则必须自己添加它。
你可以试试这个
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back"style:UIBarButtonItemStylePlain target:self.navigationItem.backBarButtonItem action:@selector(dismissModalViewControllerAnimated:)];
您正在将新控制器显示为模态视图控制器。 模态控制器呈现其最顶层。 你应该:
[self.navigationController pushViewController:navController animated:YES];
将View Controller推入堆栈,然后您将看到Back按钮。
阅读有关介绍视图控制器的Apple文档: https : //developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
编辑没有看到调用视图控制器不是导航控制器的一部分。 在这种情况下,您将必须手动创建后退按钮,并将其设置为左栏导航项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.