我在viewcontrollers
中使用不同的storyboard
制作了masterdetailview,并使用推送segue
将每个链接相关联。现在当我点击列表项时,新的DetailViewController
会打开,但每一个都没有navigationbar
。我在每个视图的顶部添加了navigation item
,为每个视图添加了标题,但毕竟navigation bar
未显示。我正在使用iPad
应用,并且在两个方向上都缺少导航栏。
修改
创建Master-Detail项目时,我的appDelegate与模板相同:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}
return YES;
}
我的一个DetailViewControllers:
#import "MediaExpDetailViewController.h"
@interface MediaExpDetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@end
@implementation MediaExpDetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)viewDidLoad{
[super viewDidLoad];
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
barButtonItem.title = NSLocalizedString(@"MasterButton", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
@end
答案 0 :(得分:9)
您的DetailViewController必须连接到UINavigationController。如果您从Xcode的“New Project”菜单中设置一个新的master-detail-project,您将得到:
注意:每个Detail视图控制器必须连接到UINavigationController(直接或间接)。
编辑:要连接多个UINavigationControllers,请执行以下操作: 您的初始详细信息视图UINavigationController与拆分视图相关(像往常一样)。其他每个细节视图UINavigationController都与主视图连接(使用替换segue ):
如果您想让用户切换回初始详细信息视图,您还必须将初始视图控制器与主设备连接(因此具有两个连接,一个连接到分离,一个连接到主设备)。
要在详细视图控制器之间切换,请在主视图控制器中调用performSegueWithIdentifier:sender:
。
编辑2:
要将详细视图添加为拆分视图的委托,请实现以下方法,该方法早于viewDidLoad
- (void)awakeFromNib
{
self.splitViewController.delegate = self;
}