我創建了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]; –