2017-02-09 56 views
1

預計:畫面滯後時UISearchController駁回

當一個UIButton被竊聽,表明有一個搜索控制器的tableview與結果的視圖 - 控制模態。

當在列表中的某個項目上點擊時,將搜索欄的文本更改爲點擊的內容,然後將UIButton設置爲該文本的視圖控制器退回原始狀態。

實際:

的UIButton調用SEGUE到searchViewController。 searchViewController顯示並正確配置searchController和tableView。 單擊細胞調用退出segue展開到原始屏幕,並正確更新UIButton和搜索欄中的文本...

但是,一個令人毛骨悚然的白色屏幕滯後於放鬆segue,它使我瘋狂。

緩解嘗試:

  1. 辭去searchController然後調用SEGUE 編程
  2. 調用self.dismiss(動畫:真正完成:無)didSelectRowAt
  3. 把解僱主線程與:DispatchQueue.main.async {}
  4. 調用self.presentingViewController?.dismiss(animated:true)

視頻Demo of flashing

代碼:

SearchDetailsViewController - 該視圖 - 控制放鬆到

import UIKit 

class SearchDetailsViewController: UIViewController { 

    @IBOutlet weak var destinationButton: UIButton! 
    override func viewDidLoad() { 
    super.viewDidLoad() 
    } 

    override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if searchDestination != "" { 
     destinationButton.setTitle(searchDestination, for: UIControlState.normal) 
     destinationButton.setTitleColor(UIColor.black, for: UIControlState.normal) 
    } else { 
     destinationButton.setTitle("Search Nearby", for: UIControlState.normal) 
    } 
    } 

    @IBAction func unwindToSearchDetailsViewController(segue: UIStoryboardSegue){ 
    } 
} 

SearchViewController - 問題的孩子。我目前有tableview單元格作爲故事板中的退出segue。

class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate, UISearchControllerDelegate { 


    @IBOutlet weak var searchResultsTableView: UITableView! 

    var destinationsObj:[String:[String]] = [:] 
    var destinations:[String] = [] 
    var defaultDestinations:[String] = ["Search Nearby"] 
    var filteredDestinations:[String] = ["Search Nearby"] 
    var shouldShowSearchResults = false 

    var searchActive:Bool = false 
    var searchController: UISearchController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     defaultDestinations = recentSearches 
     configureTableView() 
     configureSearchController() 

    } 

    override func viewDidAppear(_ animated: Bool) { 
     // Show search bar keyboard 
     searchController.isActive = true 
     DispatchQueue.main.async { 
      self.searchController.searchBar.becomeFirstResponder() 
     } 

    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: Configure Functions 
    func configureSearchController() { 
     searchController = UISearchController(searchResultsController: nil) //nil lets the view controller also be the search results 
     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = false 

     searchController.searchBar.placeholder = "Where to?" 
     searchController.searchBar.delegate = self 

     searchController.searchBar.sizeToFit() 
     searchResultsTableView.tableHeaderView = searchController.searchBar 
     searchController.delegate = self 

    } 

    func configureTableView() { 
     searchResultsTableView.delegate = self 
     searchResultsTableView.dataSource = self 

     //searchResultsTableView.isMultipleTouchEnabled = false 
    } 

    // MARK: TableView Delegate Functions 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if shouldShowSearchResults { 
      return filteredDestinations.count 
     } else { 
      return defaultDestinations.count 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "idCell", for: indexPath) 

     if shouldShowSearchResults { 
      cell.textLabel?.text = filteredDestinations[indexPath.row] 
     } else { 

      cell.textLabel?.text = defaultDestinations[indexPath.row] 
     } 

     return cell 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 40.0 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 


     if let value = tableView.cellForRow(at: indexPath)?.textLabel?.text { 
      self.searchController.searchBar.text = value 
      searchDestination = value 
      if !recentSearches.contains(value) { 
       recentSearches.append(value) 
      } 

     } 
     //self.searchController.resignFirstResponder() 
//  tableView.deselectRow(at: indexPath, animated: false) 
//  DispatchQueue.main.async { 
//   self.dismiss(animated: true, completion: nil) 
//  } 

     // self.performSegue(withIdentifier: "cancelSearchSegue", sender: self) 
    } 

    // MARK: UISearchBar Delegate Functions 
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
     searchBar.resignFirstResponder() 
     //self.dismiss(animated: true, completion: nil) 
     self.performSegue(withIdentifier: "cancelSearchSegue", sender: self) 
    } 

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 

     if let value = searchBar.text { 
      searchDestination = value 
      if !recentSearches.contains(value) { 
       recentSearches.append(value) 
      } 
     } 


     //self.dismiss(animated: true, completion: nil) 
     self.performSegue(withIdentifier: "cancelSearchSegue", sender: self) 
    } 

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { 

    } 


    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
     shouldShowSearchResults = true 
     if searchText.characters.count > 1 { 
      return 
     } else { 
      if let firstLetter = searchText.characters.first{ 
       print("Typed \(firstLetter)") 
       getPredictionData(firstLetter:firstLetter.description) 
      } 
     } 
    } 

    func dismissCurrentView() { 
     // self.presentingViewController?.dismiss(animated: true, completion: nil) 
     self.performSegue(withIdentifier: "cancelSearchSegue", sender: self) 
    } 

Screenshot of my storyboard

回答

0

嗯,我想我張貼的答案櫃面出現這種情況給任何人。

在ViewDidAppear,我是searchController設置爲活動 searchController.isActive =真

我開除以前好了,我需要將其設置爲無效!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if let value = tableView.cellForRow(at: indexPath)?.textLabel?.text { 
    self.searchController.searchBar.text = value 
    searchDestination = value 
    if !recentSearches.contains(value) { 
     recentSearches.append(value) 
    } 
    } 
self.searchController.isActive = false 
self.performSegue(withIdentifier: "cancelSearchSegue", sender: self) 
} 
0

不要執行SEGUE,嘗試直接和駁回設置我找到了答案張貼不是很長後呈現視圖控制器搜索結果

+0

的屬性之前解僱視圖控制器。在解散之前,我需要將搜索控制器解除激活。 –