เพิ่ม UISearchController บนแถบนำทาง iOS

ฉันไม่สามารถเพิ่ม UISearch Controller บนแถบนำทาง iOS 9 ได้ .. มีหลายอย่างบนอินเทอร์เน็ต แต่ไม่มีเลยที่ทำงานเลย ..

รหัสคือ..

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    searchController.searchResultsUpdater = self;
    searchController.delegate = self;
    searchController.searchBar.delegate = self;
    searchController.hidesNavigationBarDuringPresentation = NO;
    searchController.dimsBackgroundDuringPresentation = YES;

    self.navigationItem.titleView = searchController.searchBar;

    self.definesPresentationContext = YES;

}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{}

person Aryan Kashyap    schedule 25.05.2016    source แหล่งที่มา


คำตอบ (1)


คุณควรใช้. ในโค้ดของคุณด้านบน คุณไม่ได้ตั้งค่าไว้เลย จึงไม่สามารถเริ่มต้นได้

[searchController = [UISearchController alloc] initWithSearchResultController:self]

รหัสที่นี่สำหรับ iOS 8/9:

UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self];
// Use the current view controller to update the search results.
searchController.searchResultsUpdater = self;
// Install the search bar as the table header.
self.navigationItem.titleView = searchController.searchBar;
// It is usually good to set the presentation context.
self.definesPresentationContext = YES;

หวังว่ามันจะช่วยคุณแก้ปัญหาของคุณได้ รักษาความหลงใหลของคุณไว้! ขอให้โชคดี.

person Cuong Nguyen    schedule 25.05.2016
comment
คุณได้ลองเพิ่ม self.definesPresentationContext = YES ใน ViewDidLoad แล้วหรือยัง - person Timur X.; 25.05.2016