导航栏在详细侧面的主 - 细节控制器丢失

时间:2013-11-22 08:59:27

标签: ios objective-c ipad storyboard master-detail

我在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

1 个答案:

答案 0 :(得分:9)

您的DetailViewController必须连接到UINavigationController。如果您从Xcode的“New Project”菜单中设置一个新的master-detail-project,您将得到:

enter image description here

注意:每个Detail视图控制器必须连接到UINavigationController(直接或间接)。

编辑:要连接多个UINavigationControllers,请执行以下操作: 您的初始详细信息视图UINavigationController与拆分视图相关(像往常一样)。其他每个细节视图UINavigationController都与主视图连接(使用替换segue ):

enter image description here

如果您想让用户切换回初始详细信息视图,您还必须将初始视图控制器与主设备连接(因此具有两个连接,一个连接到分离,一个连接到主设备)。

要在详细视图控制器之间切换,请在主视图控制器中调用performSegueWithIdentifier:sender:

编辑2:

要将详细视图添加为拆分视图的委托,请实现以下方法,该方法早于viewDidLoad

调用
- (void)awakeFromNib
{
    self.splitViewController.delegate = self;
}