how to hide the navigation bar after dismissing from searchController - ios

I have two viewControllers ControllerA and ControllerB. Both doesn't have NavigationBar as I have hided the navigationBar in both the controllers. On ControllerB there is a a tableview and on top of that is a searchBar. On selection of any tablerow I am dismissing the controller back to ControllerA. The problem is it shows some Bar on controllerA and I don't know how to hide it.If I don't search anything and Press the back Button on controller B then no navigationBar appears on the ControllerA. But If I select something then it shows. I think its something to do with the searchBar Presentation. Here is my code
ControllerB
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Search City"
self.tableView.tableHeaderView = controller.searchBar
self.definesPresentationContext = true
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredKeys.count
} else {
return dict.count-1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
// let key = self.filteredKeys[indexPath.row]
//let dictionary = self.dict[key] as! NSDictionary
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["state_name"] as? NSString)
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_country_name"] as? NSString)
if (cityName !== "-" || shortName !== "-"){
cell.stateNameLabel.text = stateName as? String
cell.cityNameLabel.text = cityName as? String
cell.shortNameLabel.text = shortName as? String
}
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let id = Int((((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["id"] as?NSString)! as String)
let cityName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["city_name"] as?NSString)! as String
let countryShortName = (((dict["\(indexPath.item)"] as?NSDictionary)!["Country"] as?NSDictionary)!["short_country_name"] as?NSString)! as String
delegate.country(id!,cityName: cityName, countryShortName: countryShortName,departureOrArrivalSegue: departureOrArrivalSegue!)
self.navigationController!.popViewControllerAnimated(true)
}

You can just use
self.navigationController?.setNavigationBarHidden(true, animated: true)

Related

Search Results segue to other View controller in swift

I am implementing search controller programatically on navigation bar it is working fine and showing result, if I can select the tableview cell(did select method) segue is working fine . when I select the searched result it is not segue to next view controller. It is showing same view controller and tableview is reloading with data. I have show once click search result segue to next view controller
image
enter image description here
var modeldata = [ModelData]()
var FilerData = [ModelData]()
override func viewDidLoad() {
super.viewDidLoad()
self.searchcontroller = UISearchController(searchResultsController: nil)
self.searchcontroller.delegate = self
self.searchcontroller.searchBar.delegate = self
self.searchcontroller.hidesNavigationBarDuringPresentation = false
self.searchcontroller.dimsBackgroundDuringPresentation = true
self.navigationItem.titleView = searchcontroller.searchBar
self.definesPresentationContext = true
self.searchcontroller.searchResultsUpdater = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return modeldata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
let row = indexPath.row
let values = modeldata[row] as ModelData
cell.NameLabel.text = values.name
return cell
}
func updateSearchResults(for searchController: UISearchController) {
let searchToSearch = searchController.searchBar.text
if(searchToSearch == "")
{
modeldata = self.FilerData
}
else{
modeldata.removeAll()
let itemsarray = self.FilerData
var ListArray = [String]()
for ListItems in itemsarray {
ListArray.append(ListItems.name!)
if(ListItems.name?.range(of: searchToSearch!, options: .caseInsensitive) != nil)
{
self.modeldata.append(ListItems)
}
}
}
self.TableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "next", sender:indexPath.row )
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let selectrow = sender as? Int
let name = modeldata[selectrow!].name
let nextview = segue.destination as? WekiViewController
nextview?.namestring = name!
}

How could I add UISwitch to Uitableview

I could not find any way to add switch button in my tableview
I have a search bar which works perfectly,
and now I want to add a toggle button to enable "AND" "OR" filter in my search.
import Foundation
import UIKit
class TableViewController: UITableViewController, UISearchResultsUpdating {
var appDel: AppDelegate!
// var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "my.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "my.png"))
var filteredTableData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
self.edgesForExtendedLayout = UIRectEdge.None
UIApplication.sharedApplication().statusBarHidden = true
appDel = UIApplication.sharedApplication().delegate as? AppDelegate
// tabBarItem = customTabBarItem
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
print("TableView called!!")
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
//refer to http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = ((appDel.dataFetcher?.appTitles)! as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredTableData.count
}
else if appDel.dataFetcher?.appTitles == nil {
return 0
}else{
return (appDel.dataFetcher?.appTitles?.count)!
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("appCell", forIndexPath: indexPath)
let currentItem: String = (appDel.dataFetcher?.appTitles?[indexPath.row])!
if (self.resultSearchController.active) {
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = currentItem
cell.detailTextLabel?.text = String(indexPath.row)
return cell
}
}
}
Create a table header view by dragging a UIView to the top of the tableView, just above the first prototype cell. See this question for an example.
There you can place your UISwitchView and create an outlet to your VC.

Using UISearchController to search a UITableView in a different View Controller

I'm trying to implement a UISearchController to reference and search for items in a UITableView from another View Controller. In other words, I'd like to have search bar in VC1 to search for items in the UITableView of VC2.
The reason I'm doing this is because I'd like to have search functionality right when my app opens, i.e. in VC1 instead of having to go VC1->V2 to search for items in VC2.
I have already created some simple code to search for items in the UITableView of VC1, but how can I reference the UITableView of VC2 instead?
Here's my sample code for UISearchController implementation:
let tableData = ["One","Two","Three","Twenty-One"]
var filteredTableData = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active && self.resultSearchController.searchBar.text != "") {
return self.filteredTableData.count
}
else {
return self.tableData.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
if (self.resultSearchController.active && self.resultSearchController.searchBar.text != "") {
filteredTableData = tableData
cell.textLabel?.text = filteredTableData[indexPath.row]
return cell
}
else {
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)
let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredTableData = array as! [String]
self.tableView.reloadData()
}
FYI: Both my VC1 and VC2 are connected via a UINavigationController
Thanks!

Search Bar controller in TableView

Hello I am trying to implement a SearchBarController but If I type something in the search bar data doesn't comes up according to the letters typed. Here is my code
class CountriesTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filterTableData = NSDictionary()
var resultSearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
getCountriesNamesFromServer()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(self.resultSearchController.active){
return self.filterTableData.count-1
}else {
return dict.count-1
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountriesTableViewCell
if(self.resultSearchController.active){
// (cell.contentView.viewWithTag(1) as! UILabel).text = filterTableData[indexPath.row]
cell.countryNameLabel.text = (((filterTableData["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}else{
cell.countryNameLabel.text = (((dict["\(indexPath.item)"] as?NSDictionary)!["Countries"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let keys: NSArray = dict.allKeys
let filteredKeys: [String] = keys.filteredArrayUsingPredicate(NSPredicate(format: "SELF CONTAINS[c] %#", searchController.searchBar.text!)) as! [String]
filterTableData = dict.dictionaryWithValuesForKeys(filteredKeys)
}
I think Problem is in this function updateSearchResultsForSearchController because first I think I have to remove the object from Nsdictionary. I know in Array we do this
filterTableData.removeAll(keepCapacity: false)
and I think for NSDictionary I have to use NSMUtableDictionary But I don't know if that is the problem. If it is the problem still don't know how can I remove objects from NSMutableDictionary and in short to make this search functionality workable.
Note: Data is Populating successfully in tableView.. just the search functionality is not working

How do I present a view controller from UISearchViewController embedded in a child view controller of a parent view controller?

I have a parent view controller called MainViewController, and a child view controller called FilterViewController that takes partial of the main controller screen.
Inside the filterViewController I added a UISearchController to the tableview. But When I click on the search result to go to DetailViewController
Attempt to present <MyApp.DetailViewController: 0x7f97ae057780> on <MyApp.FilterViewController: 0x7f97abd62b40> which is already presenting <UISearchController: 0x7f97abd85270>
I am using this on iPad iOS 8 simulator.
If the resultSearchController is not active I can click the table and it goes to whatever I present, but somehow this uisearchcontroller won't allow me, what is a workaround to this?
Code:
MainViewController.swift
func addFilterViewController(){
filterViewController = self.storyboard?.instantiateViewControllerWithIdentifier("filterviewcontroller") as! FilterViewController
filterViewController.mainViewController = self
self.addChildViewController(filterViewController)
filterViewController.view.frame = CGRectMake(self.view.frame.width*2/3, 0, self.view.frame.width/3, self.view.frame.height)
self.view.addSubview(filterViewController.view)
filterViewController.didMoveToParentViewController(self)
}
FilterViewController.swift
func setUpSearchBar() {
resultSearchController = UISearchController(searchResultsController: nil)
resultSearchController.searchResultsUpdater = self
resultSearchController.dimsBackgroundDuringPresentation = false
let searchBar = self.resultSearchController.searchBar
searchBar.sizeToFit()
searchBar.backgroundImage = UIImage()
searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundColor = UIColor.lightBlue()
searchBar.placeholder = "Type to search"
filterTable.tableHeaderView = searchBar
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if resultSearchController.active {
println("search table select")
let detailVC = self.storyboard?.instantiateViewControllerWithIdentifier("detailViewController") as! DetailViewController
detailVC.card = filteredCards[indexPath.row]
// this has same problem if I pass in the MainViewController and do mainViewController.presentViewController...
self.presentViewController(detailVC, animated: false, completion: nil)
} else {
// other code
}
filterTable.deselectRowAtIndexPath(indexPath, animated: true)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
self.filteredCards = self.unorderedCards.filter{
(card: Card) -> Bool in
let name = card.firstName + card.lastName
let stringMatch = tributeName.lowercaseString.rangeOfString(searchController.searchBar.text.lowercaseString)
return (stringMatch != nil)
}
filterTable.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if resultSearchController.active {
return filteredCards.count
} else {
return unOrderedCards.count
}}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("filtercell", forIndexPath: indexPath) as! FilterCell
cell.backgroundColor = UIColor.lightBlue()
if resultSearchController.active {
let card = filteredCards[indexPath.row]
cell.name.text = card.firstName
} else {
let card = unOrderedCards[indexPath.row]
cell.name.text = card.firstName
}
return cell
}
If the problem is the view controller is yet presenting resultSearchController, why not let resultSearchController present the new vc?
resultSearchController.presentViewController(detailVC, animated: false, completion: nil)
Then, if you need to make the dismiss from resultSearchController, just create a new class inheriting from UISearchController.

Resources