【问题标题】:Change the delegate while displaying controller in popover在弹出窗口中显示控制器时更改委托
【发布时间】:2013-12-10 06:42:30
【问题描述】:

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

它们是不同的控制器并显示在不同的选项卡中,但是当我开始在 UISearchBar 中编辑文本时,我需要在弹出窗口中显示 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;

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

【问题讨论】:

    标签: ios objective-c delegates popover


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      要使listViewController 上的UISearchBar 成为delegatemapViewController:-

      你可以在这里做一些简单的事情,因为你有两个控制器和两个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
      }
      }
      

      【讨论】:

        【解决方案3】:

        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{
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-13
          • 2019-11-13
          • 2021-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多