ios中的多个搜索栏实现

时间:2017-11-09 07:33:10

标签: ios objective-c

我的视图控制器中有两个搜索栏... 正如预期的那样,如果我触摸第一个搜索栏,它将转到第一个搜索栏。 如果我触摸第二个搜索栏,它将转到第二个搜索栏。 这段代码工作正常,但是如果我用其他具有更大或更小屏幕的iphone设备运行它。 这不起作用,因为我只使用搜索栏的框架作为标识符..

如果有其他方法来识别哪个搜索栏被点击但是我没有找到任何类似的实现,我已经搜索了很多... 当我使用函数searchBarShouldBeginEditing时,无论如何都要识别搜索栏。

谢谢!

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
    {
    NSLog(@"searchBarShouldBeginEditing");
    NSLog(@" Description is: %@", NSStringFromCGRect(searchBar.frame));


    if (([NSStringFromCGRect(searchBar.frame) isEqualToString:@"{{108, 27}, {267, 56}}"]) || ([NSStringFromCGRect(searchBar.frame) isEqualToString:@"{{108, 33}, {559, 56}}"]))
    {
        NSLog(@"to");
        [self performSegueWithIdentifier:@"toSearchSegue" sender:self];
    }
    else if(([NSStringFromCGRect(searchBar.frame) isEqualToString:@"{{108, -3.5}, {267, 56}}"]) || ([NSStringFromCGRect(searchBar.frame) isEqualToString:@"{{108, -3.5}, {559, 56}}"]))
    {
        NSLog(@"from");
        [self performSegueWithIdentifier:@"fromSearchSegue" sender:self];
    }
    else
    {
        [self performSegueWithIdentifier:@"searchSuggest" sender:self];
    }
    return YES;
}

1 个答案:

答案 0 :(得分:1)

您可以为每个UISearchBars设置两个出口,如下所示。

 @IBOutlet weak var firstSearchBar:UISearchbar!
 @IBOutlet weak var secondSearchBar:UISearchbar!

检查委托方法中单击了哪一个。使用下面的代码段

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{


if (searchBar == firstSearchBar){
 // perform actions for first bar
   NSLog(@"to");
   [self performSegueWithIdentifier:@"toSearchSegue" sender:self];
}

if (searchBar == secondSearchBar){
// perform actions for second search bar
   NSLog(@"from");
  [self performSegueWithIdentifier:@"fromSearchSegue" sender:self];
}



return YES;