2012-05-07 160 views
0

我創建了SearchBarViewUIView的子類,並添加了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]; 
+0

我們可以看到代碼,您添加SearchBarView到屏幕上,你試圖將其刪除,請。 –

+0

我添加了添加SearchBarView的代碼。我在searchBarSearchButtonClicked上面移除它。它應該像這樣工作,對吧? – maxgrinev

+0

你有沒有試過[super removeFromSuperview]; –

回答

-2

嘗試

[searchbar removefromsuperview]; 
+0

已經嘗試過。沒有幫助。 – maxgrinev

0

你的問題是

[self addSubview:querySearchBar]; 

如果添加searchcontroller到子視圖,那麼當你做這些這是行不通的。

[searchController.searchBar removeFromSuperview()] 

self.definesPresentationContext = true 

searchController.active = false 

我建議你做

presentViewController(searchController, animated: true, completion: nil) 

並嘗試解僱searchController

[searchController.searchBar removeFromSuperview()]