2015-06-28 58 views
7

我在表視圖控制器中創建了一個UISearchController。我使用另一個視圖控制器的push segue繼續使用這個表視圖控制器。只要按下表格視圖控制器,我就希望鍵盤在搜索欄中顯示光標。當UISearchController加載時自動顯示鍵盤

我做了使用

self.mySearchController.active = true 

它確實讓搜索控制器活躍,但這並不帶來了鍵盤也不是光標放置在搜索欄搜索控制器活躍在viewDidLoad方法。我也試過

self.mySearchController.searchBar.becomeFirstResponder() 

這條線似乎沒有任何效果。

如何自動/編程調出鍵盤?下面是我的代碼更詳細的版本

class PickAddressViewController: UITableViewController, UISearchResultsUpdating { 

var searchText = "" 

var mySearchController = UISearchController() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.mySearchController = ({ 
     let controller = UISearchController(searchResultsController: nil) 
     controller.searchResultsUpdater = self 
     controller.dimsBackgroundDuringPresentation = false 
     controller.searchBar.sizeToFit() 
     controller.searchBar.text = self.searchText 

     self.tableView.tableHeaderView = controller.searchBar 

     return controller 
    })() 

    self.mySearchController.active = true 
    self.mySearchController.searchBar.becomeFirstResponder() 
} 

回答

3

BecomeFirstResponder是要走的路,但你應該做的不是在viewDidLoad。看看下面的討論的細節 - Cannot set searchBar as firstResponder

+4

太好了!我試圖在該鏈接的解決方案,它的工作原理!對於像我這樣的其他新手,除了我在上面的問題中編寫的代碼之外,您還必須執行以下操作:使視圖控制器成爲UISearchControllerDelegate。在定義搜索控制器時,寫一行表示controller.delegate = self的行。將becomeFirstResponder行放入名爲「didPresentSearchController」的委託函數中 – RookiePro

2

我也嘗試了尼基塔列昂諾夫提到的鏈接中列出的建議。我需要添加使類UISearchControllerDelegate & UISearchBarDelegate,然後它的工作。我不ü

class PickAddressViewController: UITableViewController, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.mySearchController = ({ 
      controller.searchBar.delegate = self 
     })() 

     self.mySearchController.active = true 
     self.mySearchController.delegate = self 
    } 

    func didPresentSearchController(searchController: UISearchController) { 
     self.mySearchController.searchBar.becomeFirstResponder() 
    } 
    … 
} 
0

除了做什麼其他用戶的建議,我也做了以下內容,它的工作:

searchController.definesPresentationContext = true

2
override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(true) 
    self.navigationItem.titleView = searchController!.searchBar 
    dispatch_async(dispatch_get_main_queue(), { 
     self.searchController?.active = true 
     self.searchController!.searchBar.becomeFirstResponder() 
    }) 
} 

和驗證碼

func presentSearchController(searchController: UISearchController) { 
    searchController.searchBar.becomeFirstResponder() 
} 

確保你在viewDidLoad()中給出 searchController?.delegate = self。經測試在iOS 9 *在我的情況下,設備

0

斯威夫特3溶液:

override func viewDidLoad() { 
    super.viewDidLoad() 
    navigationItem.titleView = mySearchController.searchBar 
    mySearchController.searchResultsUpdater = self 
    mySearchController.delegate = self 

} 

override func viewDidAppear(_ animated: Bool) { 
    DispatchQueue.main.async { 
     self.mySearchController.isActive = true 
    } 
} 

func presentSearchController(_ searchController: UISearchController) { 
    mySearchController.searchBar.becomeFirstResponder() 
} 
相關問題