iOS:尝试将导航栏添加到Modal UITableViewController

时间:2012-02-15 23:00:48

标签: objective-c ios5 uitableview

当我想要添加新项目时,我正在关注CoreDataRecipes应用程序以模拟显示添加屏幕。但是我无法让栏显示在顶部,所以我可以按“完成”或“取消”。

在调用模态控制器的xib中,我将+按钮链接到通过IB模式滑动控制器。

我的模态控制器中有以下内容

self.navigationItem.title = @"Add";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
    self.navigationController.navigationBarHidden = NO;

在我的viewDidLoad

模态控制器显示正常,但没有条形,所以我不能离开那个屏幕。

4 个答案:

答案 0 :(得分:13)

您需要在实际显示弹出窗口之前添加它。

在创建模态弹出框的位置,您需要先在UINavigationController中创建它。

所以,请执行以下操作。

 PopoverView *foo = [[PopoverView alloc] initWithNibName:@"PopoverView" bundle:nil];
 // Here you pass through properties if you need too.
 // ...
 UINavigationController *navC = [[UINavigationController alloc] initWithRootView:foo];
 [foo release];

 [self.navigationController presentModalViewController:navC animated:YES];

这将为模态视图提供您尝试编辑的导航栏。

答案 1 :(得分:4)

或者,您可以维护故事板segue。在Xcode中,选择您要转换到的视图控制器并将其嵌入导航控制器中。

现在在该视图控制器的viewDidLoad中,添加:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel)];

最后是回调:

- (void)cancel {
    [self dismissModalViewControllerAnimated:YES];
}

答案 2 :(得分:0)

或者如果您只需要条形图,而不仅仅是它的功能,您可以将导航栏(UINavigationBar)或工具栏(UIToolbar)控件从“媒体库”面板拖到您的视图上并从那里开始。

答案 3 :(得分:0)

我有一个类似的困境,我在一个containerView中加载了一个UITableViewController。 containerView位于一个以模态方式呈现的UIViewController中。

我和你一样,需要导航栏有一个标题和一个完成/取消按钮。

堆栈溢出后,我终于做到了 -

将UIView拖动为IB中表视图中的第一项。这自动占据了44分的高度并且向上突然移动。它也向下移动了我的第一部分。

我在此视图中拖动了一个UIButton(完成按钮)。为它创建了一个IBOutlet并调用

    [self dismissViewControllerAnimated:YES completion:nil];

声明:

1)这个假的导航栏将与tableview一起滚动。 2)这可能不是Bot所要求的解决方案,但对于可能正在寻找类似事物的其他人来说,这是一个选择。

相关问题