class PlaceSelectorViewController: UIViewController, GMSAutocompleteResultsViewControllerDelegate, UISearchBarDelegate, UISearchControllerDelegate {
I have UISearchBarDelegate in my class
searchController?.searchBar.delegate = self
and I had set the delegate to Self.
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
stopHighlight()
}
But this Delegate call is not working
You need an outleet for your UISearchBar (assuming you're working with Storyboard this is really easy, just drag and drop via Assistant Editor). and that's what you're going to add the delegate to. In case you built programmatically, then that variable is the one that's going to get assigned to the delegate.
Here's the example code for the Storyboard built
#IBOutlet weak var searchBar: UISearchBar! (you must see a filled circle at the right of this, otherwise there's no link between Storyboard and this outlet)
...
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
Related
I understand that viewDidLoad is where you are supposed to put any set up code in relation to buttons, color, and other view related code. However, in a code sample I have just seen, a reference to IBOutlet and to IBAction are not written inside of viewDidLoad but rather, just before this method, as below. Are these not set up related code, as in creating a label and a method to manipulate it?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
#IBAction func showName(sender: AnyObject) {
nameLabel.text = "my name is Cyril"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
ViewDidLoad is only one method of setup. You can put some code here, local variables, ets. #IBOutlet and #IBAction are links of Interface Builder and nameLabel. And this outlet and action suppose to be global, so you could use them in other functions, not only in viewDidLoad.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = "my name is Cyril"
}
}
I am implementing a search feature in my app. The app consists of a view controller and a custom class that handles the search logic. This custom class is called SearchController.
My goal is to make the searchBar notify the view controller when the user is about to begin searching (exactly the same behaviour as the UISearchBarDelegate method searchBarShouldBeginEditing).
Normally, you would just declare searchBarShouldBeginEditing inside SearchController but I am trying to call this method from inside the viewController because I want something in my view to change when this event happens (and thus the viewController should be handling it, not the searchController).
SearchController class:
class SearchController: NSObject, UISearchBarDelegate {
let searchBar = UISearchBar()
var searchButton = UIBarButtonItem? = nil
/* Other irrelevant class properties */
func setup() {
searchBar.delegate = self
/* other setup */
}
}
ViewController class:
class ViewController: UIViewController {
private let searchController = SearchController()
override func viewDidLoad() {
super.viewDidLoad()
searchController.delegate = self
searchController.setup()
}
/* setup a tableview to display results... this part of the implementation works fine */
}
I omitted the majority of these two classes because the search feature already works. The only thing I am struggling with is finding a way to let viewController know when the user is about to begin typing into the search field.
I tried making viewController implement UISearchBarDelegate but I am already making SearchController implement UISearchBarDelegate so why can't I access the delegate methods inside viewController?
I hope I made myself clear, I can clarify this post further if necessary. I have been tearing my hair out trying to figure this out on my own.
Ok, a searchBar cannot have 2 delegates, so you're gonna have to find a way to work around that.
One way to go about this is this:
protocol SearchControllerDelegate: class{
func searchBarDidBeginEditing()
}
class SearchController: NSObject, UISearchBarDelegate {
weak var delegate: SearchControllerDelegate?
private let searchBar = UISearchBar()
func setup() {
searchBar.delegate = self
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
delegate?.searchBarDidBeginEditing()
}
}
class ViewController: UIViewController, SearchControllerDelegate{
var searchController = SearchController()
func setUP(){
self.searchController.delegate = self
}
func searchBarDidBeginEditing() {
/// perform some action here
}
}
You can use UISearchController as rmaddy suggested, implement UISearchResultsUpdating
ViewController class:
class ViewController: UIViewController, UISearchResultsUpdating {
private let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.delegate = self
....
}
// Called when the search bar's text or scope has changed or when the search bar becomes first responder.
func updateSearchResults(for searchController: UISearchController) {
//Do something...
}
}
Or if you really want to implement the search bar logic yourself, you can go with the closure:
SearchController class:
class SearchController: NSObject, UISearchBarDelegate {
let searchBar = UISearchBar()
var searchButton = UIBarButtonItem? = nil
var didBeginSearching: (() -> ())?
/* Other irrelevant class properties */
func setup() {
searchBar.delegate = self
/* other setup */
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
didBeginSearching?()
}
}
ViewController class:
class ViewController: UIViewController {
private let searchController = SearchController()
override func viewDidLoad() {
super.viewDidLoad()
searchController.setup()
searchController.didBeginSearching = { [weak self] in
//Do something...
}
}
}
I have set the delegate in the viewDidLoad() method and also checked the IB to see if the delegate shows up when using (control + drag) and it does infact show as being hooked up. I deleted it as well and added a new SearchBar entirely in IB and hooked it back up. Nothing seems to do the trick. Any suggestions?
override func viewDidLoad() {
self.searchBar.layer.zPosition = 1
self.searchBar.delegate = self
}
//this is never being called, breakpoint never hits
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
}
//this is never being called, breakpoint never hits
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
print("search button clicked")
self.firebaseQuery()
}
Is your custom View Controller class set in Interface Builder? I.e. on the Identity Inspector tab, ensure your desired UIViewController subclass is set in the Custom Class section.
Also, try setting a breakpoint in viewDidLoad(). Run the app and if the breakpoint doesn't get hit when you expect, that helps narrow down the problem.
Is it possible you're setting the delegate incorrectly?
In Interface Builder, command-click the search bar, drag the delegate to the yellow ViewController button in the storyboard to set the delegate.
Remove self.searchBar.delegate = self from viewDidLoad()
I'm also not seeing if your viewController Class has properly conformed to UISearchBarDelegate I would add it in an extension to your controller and put all your code relating to it there.
class ViewController: UIViewController{...}
extension ViewController: UISearchBarDelegate {
// implement all searchBar related methods here:
func searchBarButtonClicked(_ searchBar: UISearchBar) {
//capture the string
if let input = searchBar.text {
// do something with string.
print(input)
}
}
I also had a problem with it not responding when I set it up using IB. I finally set it up using code instead of IB, and it worked. Try it this way:
class MySearchTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationType.none
self.navigationItem.searchController = self.searchController
//if this is set to true, the search bar hides when you scroll.
self.navigationItem.hidesSearchBarWhenScrolling = false
//this is so I'm told of changes to what's typed
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.delegate = self
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
//hopefully this gets called when you click Search button
}
func updateSearchResults(for searchController: UISearchController) {
//if you want to start searching on each keystroke, you implement this method. I'm going to wait until they click Search.
}
https://www.reddit.com/r/swift/comments/85o75h/bug_uisearchbar_delegate_methods_not_being_called/dvzcbb4/
Thanks to reddit user #applishish I got the issue
I did not put a underscore in front of the parameter -_-
Thanks all for your help!
I have AllOrganizacionUIView class, I used UISearchBarDelegate delegate, on workink
class AllOrganizacionUIView: UIView , UISearchBarDelegate {
#IBOutlet var SearchBar: UISearchBar!
var searchActive : Bool = false
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
aa.SearchBar.endEditing(true)
print("hajox")
}
}
I Have use UISearchBarDelegate delegate in ViewController class,
class ViewController: UIViewController{
#IBOutlet var SearchBar: UISearchBar!
// not working
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
self.CircleImageView_3.image = UIImage(named: "FullCircle")
self.CircleImageView_2.image = UIImage(named: "Circle")
self.CircleImageView_1.image = UIImage(named: "Circle")
self.mMainCategory.hidden = true
self.mAllCategores.hidden = true
self.mAllOrganizations.hidden = false
self.FooterViewController.text = "Основные категории"
return true
}
}
Help me please with this problem.
You have to set delegate for your search bar, possibly in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
self.SearchBar.delegate = self
}
But it will be much better if you point the delegate on storyboard instead of code.
So, I see two problems in your code. First, you mean that your delegate is an UIView subclass. But then you mean that you want to use delegate which is implemented in UIViewController subclass.
You're able to have only one delegate at the same time. Choose one (view controller is preferred), and implement all delegate methods there. Then just set delegate as I said at the beginning of my answer.
Actually, I suggest you to read about protocols and delegates at the official Apple's documentation website. It is really necessary to read documentation carefully, because there are a lot of unique things in iOS that are different to another platforms.
override func viewDidLoad() {
super.viewDidLoad()
self.SearchBar.delegate = self
}
or set your delegate in storyboard .
So I have a UITextView and some placeholder text inside. When the user taps inside the the view, I want to execute some code, i.e. clear the placeholder text. I was trying to create an IBAction but it won't let me. I looked it up online and found this UITextViewDelegate Protocol Reference but I can't figure out how to use it. A lot of the examples I've found for working with delegates are Objective-C and I am working in Swift.
Sorry for the simple question I'm new at this.
Thanks!
Given an IBOutlet to a text view someTextView, all you need to do is make your class conform to UITextViewDelegate, set that text view's delegate to self, and implement the textViewDidBeginEditing method:
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var someTextView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
someTextView.delegate = self
}
func textViewDidBeginEditing(textView: UITextView) {
println("Some code")
}
}
The View Controller should adhere to UITextViewDelegate. Then make sure to implement textViewDidBeginEditing delegate methods. The below code should clear the default place holder text when the user starts editing the textview.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textViewDidBeginEditing(textView: UITextView) {
self.textView.text = ""
}
}