![](/img/trans.png)
[英]What is the best way to switch views with UIViewController and UIViews without interface builder?
[英]Howto implement UISearchDisplayController in a UIViewController without Interface Builder
我有一個UIViewController
,它有一個分組的UITableView
作為屬性。 我在代碼中實例化UITableView
並且不使用IB。 我想連接一個UISearchDisplayController
,但無法找到如何做到這一點的任何示例。
這就是我所擁有的。 //已在頭文件中實現了UISearchDisplayDelegate
//SearchBar
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
searchBar.barStyle=UIBarStyleBlackTranslucent;
searchBar.showsCancelButton=NO;
searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
searchBar.delegate=self;
UISearchDisplayController *mySearchDisplayController = [[UISearchDisplayController alloc ]initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController = mySearchDisplayController; //Error here ?? self.searchDisplayController is ReadOnly and can't assign
[self.searchDisplayController setDelegate:self];
[self.searchDisplayController setSearchResultsDataSource:self];
[mySearchDisplayController release];
[myDisplayController release];
這似乎不起作用, UIViewController
的searchDisplayController屬性似乎是readonly,我無法將myDisplayController
掛鈎到它上面。 我真的不確定這是否是正確的方法。
我一直在谷歌周圍找到一些關於如何在UIViewController
使用UISearchDisplayController
技巧或教程。 我能找到的所有示例都是如何使用IB將其實現到UITableViewController
中,這不是我想要使用它的方式。
任何人都可以解釋我怎么能讓這個工作?
這是我使用的代碼。 把它放在一個UIViewController的viewDidLoad中,它實例化它自己的UITableView。 我認為您正在尋找的部分是將搜索欄添加為表格視圖的標題視圖。
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = @"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = YES;
self.theTableView.tableHeaderView = theSearchBar;
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
self.searchController = searchCon;
[searchCon release];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
[searchController setActive:YES animated:YES];
[theSearchBar becomeFirstResponder];
請參閱Apple文檔:
@property(nonatomic, readonly, retain) UISearchDisplayController *searchDisplayController
討論:此屬性反映您在Interface Builder中設置的searchDisplayController插座的值。 如果以編程方式創建搜索顯示控制器,則在初始化時,搜索顯示控制器會自動設置此屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.