【问题标题】:iOS: Trying to add Navigation Bar to Modal UITableViewControlleriOS:尝试将导航栏添加到模态 UITableViewController
【发布时间】:2012-02-15 23:00:47
【问题描述】:

我正在关注 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;

在我看来DidLoad

模态控制器显示正常,但没有栏,所以我无法离开该屏幕。

【问题讨论】:

    标签: objective-c ios5 uitableview


    【解决方案1】:

    您需要在弹出框实际出现之前添加它。

    在您创建模态弹出框的地方,您需要先在 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];
    

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

    【讨论】:

    • 是的,这一切都是以编程方式完成的。它会在您用来显示模态视图的任何方法中。
    【解决方案2】:

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

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

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

    最后是回调:

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

    【讨论】:

    • 如果您需要prepareForSegue UIViewController 看看这个solution。您不能直接使用destinationViewController,因为它指向您的to UIViewController 所在的UINavigationController
    【解决方案3】:

    或者,如果您只需要栏的外观,而不是其功能,您可以将导航栏 (UINavigationBar) 或工具栏 (UIToolbar) 控件从媒体库面板拖到您的视图上,然后从那里开始。

    【讨论】:

      【解决方案4】:

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

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

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

      在 IB 的 Table View 中拖动一个 UIView 作为第一项。这会自动占用 44 pts 的高度并捕捉到顶部。它也将我的第一部分向下移动。

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

          [self dismissViewControllerAnimated:YES completion:nil];
      

      免责声明:

      1) 这个假导航栏会随着表格视图滚动。 2)这可能不是 Bot 所要求的解决方案,但对于可能正在寻找类似内容的其他人来说,这是一个选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-16
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-07-20
        相关资源
        最近更新 更多