14

I have a UIViewController that is a UISearchBarDelegate and a MKMapViewDelegate. The searchBarSearchButtonClicked event works fine, but when testing in iOS 4.2 the searchBarCancelButtonClicked never gets called when hitting the cancel button. In 4.3 everything works fine. I have other views with identical code and it works fine. I have triple checked the method signatures.

Could it be something to do with the MapView, or am I doing something blatantly wrong?

My .h file:

@interface MyViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate,UIAlertViewDelegate>{
MKMapView *mapMainView;
UISearchBar *sBar;

}

@property (nonatomic, retain) UISearchBar *sBar;
@end

And I create the search bar like so:

sBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)] autorelease];
sBar.delegate = self;
sBar.showsCancelButton = YES;
[self.view addSubview:sBar];
[sBar becomeFirstResponder];

The method:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
searchBar.hidden = YES;
}

Does anyone have an idea of why this may be happening?

4

4 に答える 4

21

私はまったく同じ問題を抱えていました。キャンセルボタンを数秒間押し続けるとうまくいきました。

私の理由は、テーブルビューに UITapGestureRecognizer を実装したからです。そのため、これは検索バーのボタン クリックまたは「x」ボタン クリックよりも優先されました。

私の場合の解決策は、ジェスチャー認識をテーブルビューの背景ビューのみに制限することでした。あなたのケースでも同様のことが起こっているのではないかと思います。ジェスチャ レコグナイザーを必要最小限のサブビューに制限するようにしてください。検索バーはそのビューの外にある必要があります。

于 2011-09-07T19:25:42.947 に答える
4

おそらくあなたのsbarオブジェクトはリリースしています。この場合は自動リリースオブジェクトです、なぜですか?sBarをIBOutletプロパティとして宣言してみてください。Interface Builderで適切なリンクを作成し、コーディング時に割り当てを削除して、viewDidUnloadに配置しますself.sbar = nil; そしてそれをdeallocでリリースします。viewDidLoadにこれを置きます。

sBar.delegate = self;
sBar.showsCancelButton = YES; // this is an option in object inspector
[self.view addSubview:sBar];
[sBar becomeFirstResponder]; //remove this.

それが機能するかどうか教えてください

于 2011-05-19T11:34:54.580 に答える
1

これを試して:

sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)];

sBar.delegate = self;

sBar.showsCancelButton = YES;

[self.view addSubview:sBar];

そしてリリースをdeallocに入れてみてください

于 2011-07-23T06:32:33.383 に答える
1
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar resignFirstResponder];
    }
}
于 2013-04-12T04:41:51.470 に答える