我创建了SearchBarView
,UIView
的子类,并添加了UISearchBar
作为其子视图。在SearchBarView
(从searchBarSearchButtonClicked
)呼叫removeFromSuperview
后,UISearchBar
不消失。我试图在SearchBarView
上拨打removeFromSuperview
从我创建SearchBarView的地方,但它没有帮助。任何想法为什么?无法删除UISearchBar
#import <UIKit/UIKit.h>
@interface SearchBarView : UIView <UISearchBarDelegate> {
UISearchBar *querySearchBar;
}
@end
#import "SearchBarView.h"
@implementation SearchBarView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
querySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,frame.size.width,44)];
querySearchBar.delegate = self;
[self addSubview:querySearchBar];
}
return self;
}
#pragma mark -
#pragma mark UISearchBarDelegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self removeFromSuperview];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = @"";
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
return YES;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
return YES;
}
@end
SearchBarView创建如下:
SearchBarView *searchBarView = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 0, 480, 300)];
UIView *rootView = [[[[UIApplication sharedApplication] delegate] window] rootViewController].view;
[rootView addSubview:searchBarView];
我们可以看到代码,您添加SearchBarView到屏幕上,你试图将其删除,请。 –
我添加了添加SearchBarView的代码。我在searchBarSearchButtonClicked上面移除它。它应该像这样工作,对吧? – maxgrinev
你有没有试过[super removeFromSuperview]; –