-1

I have the following code:

extension SegmentedControlViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
        if abs(scrollView.contentOffset.x - 10) < 1 {
            print("works")
        }
    }
}
  @IBOutlet weak var scrollView: UIScrollView!

then I call the function in the viewDidLoad like this:

scrollViewDidScroll(scrollView)

Although I am always getting the error message

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

What am I doing wrong, how to Get the scroll view to not be nil.

coder123
  • 247
  • 3
  • 17
  • Either you haven't connected `@IBOutlet` or your code is called before `viewDidLoad`. – user28434'mstep Jul 20 '20 at 15:54
  • Does this answer your question? [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – user28434'mstep Jul 20 '20 at 15:54
  • @user28434 I was just reviewing that and it didn't help me – coder123 Jul 20 '20 at 15:55
  • @user28434 also I have my outlet connected and I call the function in the viewDidLoad – coder123 Jul 20 '20 at 15:56
  • 1
    You're probably asking the wrong question. `scrollViewDidScroll()` is a delegate function, called by UIKit when the scroll view has scrolled. What are you trying to accomplish by calling it directly from `viewDidLoad()` (where it has not yet been setup by auto layout and cannot possibly have scrolled)? – DonMag Jul 20 '20 at 18:12
  • @DonMag where should I call it then? – coder123 Jul 20 '20 at 18:16
  • 1
    @coder123 - ***"What are you trying to accomplish by calling it?"*** – DonMag Jul 20 '20 at 18:21
  • @DonMag all I am trying to accomplish printing "works" to the console when the content offset is more than 10 – coder123 Jul 20 '20 at 18:25
  • 1
    @coder123 - then assign the delegate of your scroll view, and start scrolling. – DonMag Jul 20 '20 at 18:26
  • @DonMag I'm sort of new to coding what do you mean by that – coder123 Jul 20 '20 at 18:27

1 Answers1

1

Assign the scroll view's .delegate in viewDidLoad() and start scrolling:

class SegmentedControlViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // assign the delegate      
        scrollView.delegate = self
        
    }

}

extension SegmentedControlViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
        if abs(scrollView.contentOffset.x - 10) < 1 {
            print("works")
        }
    }
}

Edit

If you want to check if the scroll view content has been dragged to the right more than 10 points, use this:

extension SegmentedControlViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
        if scrollView.contentOffset.x < 10 {
            print("scroll content has been dragged to the right more than 10 points")
        }
    }
}
DonMag
  • 69,424
  • 5
  • 50
  • 86