2017-02-09 54 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

的属性之前解雇视图控制器。在解散之前,我需要将搜索控制器解除激活。 –