无法向我的导航控制器添加右侧或左侧栏按钮项

时间:2011-10-04 14:30:50

标签: iphone cocoa-touch

尝试过本网站上的所有示例我在模态视图中看不到任何内容,但我确实看到了导航栏,但它是空的

EditEntityViewController *editEntityViewController = [[EditEntityViewController alloc] init];
editEntityViewController.currentNode = newNode;
editEntityViewController.delegate = self;

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editEntityViewController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;


UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show"
                                                                  style:UIBarButtonItemStylePlain 
                                                                 target:self
                                                                 action:@selector(refreshPropertyList:)];          
editEntityViewController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];


[self presentModalViewController:navController animated:YES];

[editEntityViewController release];

4 个答案:

答案 0 :(得分:0)

尝试在editEntityViewController 之前设置rightBarButtonItem 使用initWithRootViewController创建UINavigationController:。

我认为在创建UINavigationController时会设置导航栏。在创建时间之后添加正确的条形项目为时已晚。

编辑:好的,这不是问题。

以下最小代码段可行,因此我会检查您的EditEntityViewController是否正在执行某些操作以删除其他位置的按钮:

- (IBAction)showPopup:(id)sender
{
    UIViewController *popupController = [[UIViewController alloc] init];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:popupController];
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;


    UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show"
                                                                      style:UIBarButtonItemStylePlain 
                                                                     target:self
                                                                     action:nil];       

    popupController.navigationItem.rightBarButtonItem = anotherButton;
    [anotherButton release];

    [self presentModalViewController:navController animated:YES];

    [popupController release];
}

答案 1 :(得分:0)

这不起作用的原因真是太愚蠢了。基本上我在EditViewController中定义了一个名为navigationItem的IBOutlet,它与SDK的属性名称相同。

我删除了它和笔尖的链接,而Robin说它完美无缺。

答案 2 :(得分:0)

如上所述,您的代码是正确的,并且是显示带有UINavigationBar的弹出工作表的标准方式,用于保存按钮以关闭工作表。但是,您在EditViewController中定义了一个名为navigationItem的IBOutlet,它导致了冲突。

答案 3 :(得分:-1)

导航控制器上模态显示的视图控制器没有navigationItem和navigationController属性。但是,它们具有parentViewController属性,但这与您的情况无关。

如果要在模态显示的视图上自定义导航栏,则应将IBOutlet从管理该视图的视图控制器连接到放置在该管理视图中的导航栏。然后通过IBOutlet实例变量进行操作。