简体   繁体   English

如何让子视图控制器将 UISearchBar 添加到父级的 UINavigationController?

[英]How to let child view controller add UISearchBar to parent's UINavigationController?

How to let child view controller modify navigation controller?如何让子视图控制器修改导航控制器?

I am faced with this problem right now, and have not been able to find a solution that works, let alone one that works well.我现在面临这个问题,一直无法找到有效的解决方案,更不用说有效的解决方案了。

Basically, right now I have a UINavigationController that contains a UITableViewController with a search bar in the navigation bar.基本上,现在我有一个UINavigationController ,它包含一个UITableViewController ,在导航栏中有一个搜索栏。 So far so good - everything works well.到目前为止一切顺利 - 一切正常。

However, I would like to show some custom views between the navigation bar and the top of the table view at times.但是,有时我想在导航栏和表格视图顶部之间显示一些自定义视图。 To do this, I figured it would be best to create another view controller (let's call it the ContainerViewController ), which is presented by the navigation controller.为此,我认为最好创建另一个视图控制器(我们称之为ContainerViewController ),它由导航控制器呈现。 This container view controller holds the table view controller as a child, and can insert any custom view it wishes to.这个容器视图控制器将表视图控制器作为子视图,并且可以插入它想要的任何自定义视图。 The view controller hierarchy should look like this:视图控制器层次结构应如下所示:

UINavigationController
    ContainerViewController
        UITableViewController (with UISearchDisplayController)

Now I am faced with a problem, since the search bar of the UISearchDisplayController should be displayed in the navigation bar (by calling didSetupSearchDisplayController ), but I obviously want all logic pertaining to it kept in the UITableViewController .现在我面临一个问题,因为UISearchDisplayController的搜索栏应该显示在导航栏中(通过调用didSetupSearchDisplayController ),但我显然希望与其相关的所有逻辑都保存在UITableViewController I need the container view controller to sort of act as a go-between here.我需要容器视图控制器在这里充当中间人。 I have tried to figure this out, but haven't managed a solution yet.我试图解决这个问题,但还没有找到解决方案。

Here is how I instantiate the table view controller and add it as a child in the container view controller.下面是我如何实例化表视图控制器并将其添加为容器视图控制器中的子项。

- (void)viewDidLoad {
    [super viewDidLoad];

    // Load view controller from storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    self.contentViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyListViewController"];
    self.contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

    // Add its view to the content view
    [self addChildViewController:self.contentViewController];
    [self.container addSubview:self.contentViewController.view];
    [self.contentViewController didMoveToParentViewController:self];

    // Set up constraints
    [self.contentViewController.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}

In the child view controller, I have noticed that self.navigationController is nil in viewDidLoad but not in didMoveToParentViewController .在子视图控制器中,我注意到self.navigationControllerviewDidLoad为零,但在didMoveToParentViewController没有。 So the following works:所以以下工作:

- (void)didMoveToParentViewController:(UIViewController *)parent {
    // Hide separator below navigation bar
    self.navigationController.navigationBar.clipsToBounds = YES;
}

But I cannot set the search bar to display in the navigation bar, nor can I set the navigation bar title (with self.title = ... ) from within the child view controller.但是我无法将搜索栏设置为在导航栏中显示,也无法从子视图控制器中设置导航栏标题(使用self.title = ... )。

Would appreciate any help.将不胜感激任何帮助。

EDIT: Here is some code I use in the UITableViewController (the contained view) to create the search display controller:编辑:这是我在UITableViewController (包含的视图)中使用的一些代码来创建搜索显示控制器:

UISearchBar *searchBar = [[UISearchBar alloc] init];
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];

// Show search bar in navigation bar (instead of in its own bar underneath)
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;

// Set self as delegate to search display controller and its table view
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDelegate = self;

I have tried doing this both in viewDidLoad , where the self.navigationController is nil, and in didMoveToParentViewController , where the navigation controller exists, but it does not seem to matter.我已经尝试在viewDidLoad执行此操作,其中self.navigationController为零,并且在didMoveToParentViewController ,其中存在导航控制器,但这似乎无关紧要。 My guess is that the displaysSearchBarInNavigationBar magic happens after the child view is instantiated but before it is added as a child view (and thus gets access to the navigation controller).我的猜测是displaysSearchBarInNavigationBar魔法发生在子视图被实例化之后但在它被添加为子视图之前(从而可以访问导航控制器)。

can you use你能用吗

self.parentViewController.navigationController

to get the reference to the navigation controller from within the container view?从容器视图中获取对导航控制器的引用?

This may help you find your answer.这可能会帮助您找到答案。 We use this to set the title of our navigation controller我们使用它来设置导航控制器的标题

self.navigationItem.title=@"Name of view goes here";

You should be able to use self.navigationItem to get a handle to the navigation bar.您应该能够使用self.navigationItem来获取导航栏的句柄。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在父视图上添加子视图控制器的视图 - How to add child view controller's view over the parent view 如何将子ViewController的视图添加到父View控制器的子视图? - How to add child ViewController's view to subview of the parent View controller? 将子视图控制器的视图添加到父视图控制器的子视图 - Add a child view controller's view to a subview of the parent view controller 如何在iOS中将相同的子View控制器添加到父View控制器? - How to add the same child View controllers to a parent view controller in iOS? 如何将Tabbar Controller作为子视图添加到父视图? - How to add tabbar controller as child view to parent view? 如何添加一个已经有父 controller 的视图 controller 作为另一个视图 controller 的子视图 - How do I add a view controller that already has a parent controller as a child of another view controller 如何在任何子视图上方可见的UINavigationController中添加视图 - How to add a view to UINavigationController that is visible above any child view 如何从子视图控制器访问父视图控制器的视图? - how can i access a parent view controller's view from a child view controller? 如何通知父视图控制器的子视图控制器已自行删除? - How is a parent view controller notified that is's child view controller has removed itself? 将其他UINavigationController添加到子视图控制器 - Adding additional UINavigationController to a child view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM