0

I have a UIToolbar with a UISearchBar in it. When the user types something, I want a Popover to appear with the items that match the searchText in a UITableView. If there are no items that match, the TableView will be hidden and a label will appear.

Currently I am currently getting the error for the table view that it is nil.

ViewController containing the Searchbar

import UIKit

class ViewController: UIViewController, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {

@IBOutlet weak var toolbarConstraint: NSLayoutConstraint!
var searchBar:UISearchBar!
@IBOutlet weak var SearchPlaceholder: UIBarButtonItem!
var searchResultsController = SearchResultsViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    //Create the searchbar
    searchBar = UISearchBar(frame: CGRectMake(0, 0, 230, 44))
    searchBar.tintColor = UIColor(red:0, green:0, blue:0, alpha:1)
    searchBar.delegate = self
    SearchPlaceholder.customView = searchBar

    //add the keyboard notifications
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

//keyboard notifications
func keyboardWillShow(notification: NSNotification) {
    updateBottomLayoutConstraintWithNotification(notification)
}

func keyboardWillHide(notification: NSNotification) {
    updateBottomLayoutConstraintWithNotification(notification)
}

func updateBottomLayoutConstraintWithNotification(notification: NSNotification) {
    let userInfo = notification.userInfo!

    let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
    let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let convertedKeyboardEndFrame = view.convertRect(keyboardEndFrame, fromView: view.window)
    let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntValue << 16
    let animationCurve = UIViewAnimationOptions.init(rawValue: UInt(rawAnimationCurve))

    toolbarConstraint.constant = CGRectGetMaxY(view.bounds) - CGRectGetMinY(convertedKeyboardEndFrame)

    UIView.animateWithDuration(animationDuration, delay: 0.0, options: UIViewAnimationOptions.BeginFromCurrentState.union(animationCurve), animations: {
        self.view.layoutIfNeeded()
        }, completion: nil)
}

//Searchbar delegate
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SearchBar" {
        let popoverViewController = segue.destinationViewController
        popoverViewController.popoverPresentationController!.delegate = self
    }
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText != "" {
        performSegueWithIdentifier("SearchBar", sender: self)
        searchResultsController.searchForItemsWithString(searchText)
    }
    else {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {

    if searchBar.text != "" {

        performSegueWithIdentifier("SearchBar", sender: self)
        searchResultsController.searchForItemsWithString(searchBar.text!)
    }

    return true
}

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


}

Search Results ViewController

import UIKit

class SearchResultsViewController: UIViewController {

let contentArray = ["lorem", "ipsum", "dolor", "sit", "amet"]

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var noItemsLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

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

func searchForItemsWithString(searchQuery:String) {

    if contentArray.count != 0 {
        tableView.hidden = false
        noItemsLabel.hidden = true

    }
    else {
        tableView.hidden = true
        noItemsLabel.hidden = false
    }

}

}

Here is the link to my searchbar example on Github.

evenwerk
  • 947
  • 1
  • 12
  • 28
  • Have you tried turning on the "all exceptions" breakpoint to narrow down where this is happening? This is a lot of code to sort through. It would be best if you could narrow down the problem area. – Rob Norback Sep 28 '15 at 04:48
  • In the SearchResultsViewController, in the function SearchForItemsWithString(), tableview.hidden is the point where the app crashes. The function is being called from the other viewcontroller. – evenwerk Sep 28 '15 at 06:55

0 Answers0