Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My scrollViewDidScroll function is not receiving calls

I'm trying to check if a user is scrolling up and down on my app but it's not calling my scrollViewDidScroll method

Any ideas why it's not printing received scroll when I scroll up and down on the app?

import UIKit

class CreateAccount: UIViewController, UITextFieldDelegate, UIScrollViewDelegate {

    @IBOutlet weak var scrollViewer: UIScrollView!
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var reenterPasswordTextField: UITextField!

    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 scrollViewDidScroll(scrollView: UIScrollView) {
        print("received scroll")
    }

}

enter image description here

like image 298
Trevor Wood Avatar asked Sep 17 '16 16:09

Trevor Wood


3 Answers

Add the scrollview delegate. Whenever you implement a delegate method you need tell it what controller to use, usually it will be self. It's caught me out a few times.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.   
    scrollViewer.delegate = self
}
like image 163
OrderAndChaos Avatar answered Nov 20 '22 15:11

OrderAndChaos


You need to set delegate of scrollview also don't use tag to differentiate 2 scrollview instead of that create 2 outlets of scrollview and use that in UIScrollViewDelegate methods like this.

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == scrollView1 {
         print("received scrollview 1")
    }
    else if scrollView == scrollView2 {
         print("received scrollview 2")
    }
}
like image 3
Nirav D Avatar answered Nov 20 '22 15:11

Nirav D


Delegate are given properly even if your scrollview delegate method not calling then please check you delegate method which you have added for scrollview, this may give warning due to using old swift version scrollview delegate with swift 4.0

Please correct it with below if this is the case:

Old Method:

func scrollViewDidScroll(scrollView: UIScrollView) {
}

New Method:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
}
like image 3
Sandip Patel - SM Avatar answered Nov 20 '22 16:11

Sandip Patel - SM