繁体   English   中英

在弹出窗口中显示控制器时更改委托

[英]Change the delegate while displaying controller in popover

我也有一个带有UISearchBar的mapViewController和一个带有UISearchBar的listViewController。

它们是不同的控制器,并在不同的选项卡中显示,但是当我开始在UISearchBar中编辑文本时,我需要在popover中的mapViewController中显示listViewController的情况。

现在,mapViewController是mapViewController中的UISearchBar的委托,而listViewController是listViewController中的UISearchBar的委托。

所以我在mapViewController中实现了委托方法

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (!self.listView) {
        self.listView = [self.storyboard instantiateViewControllerWithIdentifier:@"ListViewController"];
        self.listView.showInPopover = YES;                  //HIDES THE UISEARCHBAR IN LISTVIEWCONTROLLER
    }
    if (!self.listViewPopover) {
        self.listViewPopover = [[UIPopoverController alloc] initWithContentViewController:self.listView];
        self.listViewPopover.passthroughViews = [NSArray arrayWithObject:searchBar];
        self.listViewPopover.delegate = self;
        [self.listViewPopover presentPopoverFromRect:searchBar.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
}

因此,现在我需要使listViewController成为mapViewController上的UISearchBar的委托。 我该如何解决? 可能吗?

我尝试添加

searchBar.delegate = self.listView;

在上述方法中-但没有效果。 等待您的帮助,谢谢。

由于您的listView也符合UISearchBarDelegate协议,因此可以将mapViewController的实现中的searchBar的委托消息转发到listView,如下所示:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    // mapViewController's normal code
    [self.listview searchBar:searchBar textDidChange:searchText];
}

要使listViewController成为mapViewController上的UISearchBardelegate ,则:

好吧,您可以在这里执行简单的操作,因为您有两个控制器和两个UISearchBar所以只需在每个搜索searchbar定义标签值并检查下面的条件,在实施下面的delegate方法之前,只需将delegate与文件fileowners连接到两个UISearchBar

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{  

NSInteger tagVal=[searchBar tag];

if (tagVal==0)
{
//impelement mapviecontroller stuff here
}
else if (tagVal==1)
{
//impelement listviewcontroller stuff here
}
}

1-在MapViewController.h中,从listViewController创建属性

@property (nonatomic,weak) ListViewController *listViewController;

2-在MapViewController.m中实现此委托

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
      [self.listViewController searchBar:searchBar textDidChange:searchText];
 }

3-在ListViewController.m中实现此委托

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM