So I have a LoginController() that is being presented from MainController(). So technically MainController() is still loaded and in memory in the background. Now in LoginController(), when I press the login button, I want to notify the MainController() that login button is pressed so fetch the current user again. Then I dismiss LoginController().
I want to do this with protocols and delegates but problem is when MainController() conforms to LoginDelegate, I do not know how to set delegate to self. Because when a user logs out or if there is no user, I do not want to go back to login controller, I want to go back to a different WelcomeController(). If I wanted to go to LoginController() then I know I can do
let loginController = LoginController()
loginController.delegate = self
self.present(loginController)
But in my case, I want to go to a completely different controller. So where do I set the delegate to self?
Here is the code:
//LoginController()
protocol LoginDelegate {
func didTapLogIn()
}
class LoginController: UIViewController, UITextFieldDelegate{
var loginDelegate: LoginDelegate!
override func viewDidLoad() {
super.viewDidLoad()
configureViews()
formValidation()
}
#objc func handleLogin(){
guard let email = emailTextField.text else {return}
guard let password = passwordTextField.text else {return}
Auth.auth().signIn(withEmail: email, password: password) { (auth, error) in
if let error = error{
print("There was a problem signing the user in", error.localizedDescription)
return
}
self.loginDelegate.didTapLogIn()
print("Successfully signed in the user")
self.dismiss(animated: true, completion: nil)
}
}
}
Here is the MainController that conforms
//MainController() which is a tab bar controller
class MainController: UITabBarController, LoginDelegate {
override func viewDidLoad() {
super.viewDidLoad()
checkLoggedInUserStatus()
}
fileprivate func checkLoggedInUserStatus(){
if Auth.auth().currentUser == nil{
DispatchQueue.main.async {
let welcomeController = UINavigationController(rootViewController: WelcomeController())
welcomeController.modalPresentationStyle = .fullScreen
self.present(welcomeController, animated: false, completion: nil)
return
}
} else {
setupTabBar()
}
}
fileprivate func setupTabBar(){
tabBar.isTranslucent = false
tabBar.barTintColor = TDGOtherBlue
let homeController = HomeController()
homeController.fetchCurrentUser()
let homeViewController = UINavigationController(rootViewController: homeController)
let chatViewController = UINavigationController(rootViewController: EditProfileController())
viewControllers = [homeViewController, chatViewController]
}
func didTapLogIn() {
checkLoggedInUserStatus()
}
}
//NOTE: ABOVE IS THE DELEGATED FUNCTION BUT WHERE DO I DO THE DELEGATE SELF DECLARATIOn
Notes:
Your architecture looks like a cocktail.
But, nonetheless, notes:
weak var loginDelegate: LoginDelegate? // should be weak and optional
Use this flow:
self.dismiss(animated: true) {
self.loginDelegate.didTapLogIn()
print("Successfully signed in the user")
}
You didn't describe what is Auth,
some you cannot be guaranteed that
Auth.auth().currentUser is not actually nil
even inside here:
#objc func handleLogin(){
//....
Auth.auth().signIn( ... {
///here
/// check it!
})
So, if not, use
protocol LoginDelegate {
func didTapLogIn(user: User)
}
instead
EDIT
Obviously, if you have nothing more while LoginViewController is being closed, you can send the data into AppDelegate.
final class AppDelegate: UIResponder {
}
extension AppDelegate: LoginDelegate { ... }
To retrieve AppDelegate shared instance you can use:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
The second solution, you can obtain your view controller using AppDelegate. This way:
let rootViewController = appDelegate.window?.rootViewController
I do not know how to set delegate to self. Because when a user logs out or if there is no user, I do not want to go back to login controller, I want to go back to a different WelcomeController().
It sounds like MainController is really running the show. You haven't said much about WelcomeController, but it sounds like the flow is something like this:
App starts up, creates and displays MainController.
If MainController doesn't have a user, it creates and displays a WelcomeController.
User taps some button in WelcomeController indicating he/she wants to log in. WelcomeController sends a message to MainController telling it to start the login process, then gets dismissed.
MainController creates a LoginController and displays it.
LoginController gets the user's credentials, sends them to MainController, and then gets dismissed.
At some point the user logs out, at which point there is no user, so go to step 2.
So the answer would be that MainController sets itself as LoginController's delegate in step 4, somewhere between creating and displaying LoginController.
let vc = FirstViewController.instantiate(fromStoryboard: .Profile)
vc.callback = { (address, lat, lon) in
self.updateLocation(address: address ?? "", lat: lat ?? 0.0, lon: lon ?? 0.0)
}
self.navigationController?.pushViewController(vc, animated: true)
Then in secondVC
var callback : (( address: String?, latitude: Double?,_ longitude: Double?)-> Void)!
Then when you want to pass the value back to previous VC, in click or dismiss event add.
callback(address,Double(lat),Double(lon))
You need to to assign delegate value from prepare function. Or you can assign delegate with initialize RedScreenVC(self) from your ViewController if u don't want to use storyboard/xib.
import UIKit
class ViewController: UIViewController, NavDelegate {
func navigate(text: String, isShown: Bool) {
print("text: \(text) isShown: \(isShown)")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "RedScreenVC") {
let redScreenVC = segue.destination as? RedScreenVC
redScreenVC?.delegate = self
}
}
#IBAction func nextPageButtonEventLustener(_ sender: Any) {
performSegue(withIdentifier: "RedScreenVC", sender: sender)
}
}
import UIKit
protocol NavDelegate {
func navigate(text: String, isShown: Bool)
}
class RedScreenVC: UIViewController {
weak var delegate: NavDelegate?
var redView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
var navigateButton: UIButton = {
let button = UIButton(frame: CGRect(x: 200, y: 350, width: 150, height: 50))
button.setTitle("Navigate", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.backgroundColor = .blue
return button
}()
#objc func buttonAction(){
if self.redView.backgroundColor == .gray {
self.redView.backgroundColor = .systemPink
}
self.delegate.navigate(text:"", isShown: true)
}
override func viewDidLoad() {
navigateButton.layer.cornerRadius = 25
redView.backgroundColor = UIColor.gray
delegate.navigate(text: "Navigation Success", isShown: true)
view.addSubview(redView)
view.addSubview(navigateButton)
}
}
If you do not want to use storyboard and initialize as like
let redScreenVC = RedScreenVC()
redScreenVC.delegate = self
class RedScreenVC: UIViewController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: nil)
self.initialize()
}
func initialize() {
self.view.backgroundColor=CustomColor.PAGE_BACKGROUND_COLOR_1
//From here you need to create your email and password textfield
}
}
I have set up a search bar using the embed nav bar. The func searchBarSearchButtonClicked is not being detected. I'd like to make it so that when the user taps on the search bar, another function will be called. I've taken out some extraneous code not relevant to this question. What could be the issue?
class FirstViewController: UIViewController, UISearchBarDelegate {
var resultSearchController: UISearchController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// set up the search results table
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearchTable") as! LocationSearchTable
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Where would you like to go"
navigationItem.titleView = resultSearchController?.searchBar
searchBar.delegate = self
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
}
func searchBarSearchButtonClicked(_: UISearchBar) {
// closeMapType()
// self.mapType.transform = .identity
print("it worked")
}
}
Below function will call when the user taps on the search bar :
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
return true
}
This will call when we click on the "Search" button in the keyboard
func searchBarSearchButtonClicked(_: UISearchBar)
I'm working on a webView based screen and I want to have an activity indicator to spin while the web content is loading.
Since I have to use the same activity indicator in another screen of the app, I've put the code in static functions in a specific file.
The activity indicator seems to work fine for some web (simple) web pages but I have an issue when I load more complex pages. The activity indicators gets duplicate several times. (See screenshot below)
On the screenshot, the first activity indicator has the correct layout but the one below is darker which implies that several other activity indicators have been overlaid on top of each other. And then they never disappears.
When it comes to code:
I have a webView and two delegate methods controlling the activity indicators.
func webViewDidStartLoad(_ webView: UIWebView) {
DispatchQueue.main.async {
EBUtil.startActivityIndicator(self)
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
DispatchQueue.main.async {
EBUtil.stopActivityIndicator(self)
}
}
I guess it comes from the fact that webViewDidStartLoad gets called several times. Any idea how I can prevent this behaviour to happen?
Thanks in advance.
Edouard
EDIT:
Here is the full code for my VC.
class NewsDetailsViewController: UIViewController, UIWebViewDelegate {
//MARK: - #IBOUTLETS
#IBOutlet weak var webView: UIWebView!
//MARK: - VARIABLES
var url: String!
//MARK: - APP LIFE CYCLE
override func viewDidLoad() {
super.viewDidLoad()
if url != nil {
let urlToLoad = NSURL(string: url)
webView.loadRequest(NSURLRequest(url: urlToLoad as! URL) as URLRequest)
}
}
func webViewDidStartLoad(_ webView: UIWebView) {
DispatchQueue.main.async {
EBUtil.startActivityIndicator(self)
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
DispatchQueue.main.async {
EBUtil.stopActivityIndicator(self)
}
}
}
And code for activity indicator start and stop:
static func startActivityIndicator(_ sender: UIViewController) {
print("START ANIMATING")
if let view = sender.view {
activityIndicatorBackground = UIView(frame: CGRect(x: view.center.x - 25, y: view.center.y - 25, width: 50, height: 50))
activityIndicatorBackground.backgroundColor = UIColor.black
activityIndicatorBackground.layer.zPosition = CGFloat(MAXFLOAT-1)
activityIndicatorBackground.alpha = 0.6
activityIndicatorBackground.layer.cornerRadius = 5
view.addSubview(activityIndicatorBackground)
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
activityIndicator.center = view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
activityIndicator.layer.zPosition = CGFloat(MAXFLOAT)
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
}
}
static func stopActivityIndicator(_ sender: UIViewController) {
print("STOP ANIMATING")
activityIndicatorBackground.removeFromSuperview()
activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
}
try this -
add below line in your viewController class -
var activityIndicator: UIActivityIndicatorView?
And update your startActivityIndicator stopActivityIndicator method like below-
func startActivityIndicator(_ sender: UIViewController) {
print("START ANIMATING")
if let view = sender.view {
if activityIndicator != nil {
activityIndicator?.removeFromSuperview()
}
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
activityIndicator?.center = view.center
activityIndicator?.layer.cornerRadius = 10
activityIndicator?.backgroundColor = UIColor.gray
activityIndicator?.hidesWhenStopped = true
activityIndicator?.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
activityIndicator?.layer.zPosition = CGFloat(MAXFLOAT)
view.addSubview(activityIndicator!)
activityIndicator?.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
}
}
func stopActivityIndicator(_ sender: UIViewController) {
print("STOP ANIMATING")
activityIndicator?.stopAnimating()
activityIndicator?.removeFromSuperview()
UIApplication.shared.endIgnoringInteractionEvents()
}
Hope it will work for you :)
I have a searchbar in my tableview header. For that i am using uisearchcontroler. But it update tablview data when i am texting in searchbar, i need to update tablview when search button in keyboard is clicked, because i get data for search in api and everytime when i am texting in searchbar it requesting and it takes long time. How i can resolve this?
var resultSearchController = UISearchController()
var indicator:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
var ref=UIRefreshControl()
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
})()
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = view.center
view.addSubview(indicator)
indicator.bringSubviewToFront(view)
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
getRequests.getType1(tableView,spinner: indicator)
ref.addTarget(self, action: #selector(Catalog1TableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
ref.attributedTitle = NSAttributedString(string: "Загрузка")
tableView.addSubview(ref)
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
indicator.startAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
tableView.reloadData()
getRequests.getProduct(tableView, spinner: indicator,name: searchController.searchBar.text!)
for item in getRequests.product {
if item.productTitle.containsString(searchController.searchBar.text!){
filteredTableData.append(item)
}
}
tableView.reloadData()
}
Try to implement UISearchBar Delegate method :
func searchBarSearchButtonClicked(searchBar: UISearchBar)
You should implement something like this in your view controller.
var resultSearchController = UISearchController()
resultSearchController.searchBar.delegate = self
extension ViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print("search button click")
}
}
I am using a search bar to use Google Place Autocomplete feature. However, it is not working when I am putting the search bar on top of GMSMapView. It works completely fine when I comment out loadView() function. Is there a way to use the place autocomplete with Google map?
import UIKit
import GoogleMaps
class ViewController: UIViewController {
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
override func loadView() {
let camera = GMSCameraPosition.cameraWithLatitude(1.285, longitude: 103.848, zoom: 12)
let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
self.view = mapView
}
override func viewDidLoad() {
super.viewDidLoad()
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRectMake(0, 65.0, 350.0, 45.0))
subView.addSubview((searchController?.searchBar)!)
self.view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.hidesNavigationBarDuringPresentation = false
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
self.definesPresentationContext = true
}
}
// Handle the user's selection.
extension ViewController: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWithPlace place: GMSPlace) {
searchController?.active = false
// Do something with the selected place.
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place attributions: ", place.attributions)
}
func resultsController(resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: NSError){
// TODO: handle the error.
print("Error: ", error.description)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictionsForResultsController(resultsController: GMSAutocompleteResultsViewController) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
The code above is from https://developers.google.com/maps/documentation/ios-sdk/map and https://developers.google.com/places/ios-api/autocomplete. I copied these for testing and is still not working.
Sorry, I don't have the reputation to write comments yet, but I think this could help you to figure out the issue.
First call the super.loadView() and when you initialize the GMSMapView you must set a frame size. Finally add the mapView to the subview.
override func loadView() {
super.loadView()
let camera = GMSCameraPosition.cameraWithLatitude(1.285, longitude: 103.848, zoom: 12)
let mapView = GMSMapView.mapWithFrame(self.view.frame, camera: camera)
self.view.addSubview(mapView)
}