0

When I instantiate and add a scroll view to my self.view in my ViewController class and set the scroll view's delegate to self, the delegate functions get called. As written below:

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var smallView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let width = Double(view.frame.width)
        let height = Double(view.frame.height)
        let frame = CGRect(x: 0.0, y: 0.0, width: width, height: height)
        let scrollView = UIScrollView(frame: frame)
        //scrollView.backgroundColor = UIColor.blue
        let size = CGSize(width: width + 300, height:1000)
        scrollView.contentSize = size
        smallView.addSubview(scrollView)
        scrollView.delegate = self

    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("Gets called.")
    }

}

However, when I create a custom class, in this case called, PhotoBooth, and call try to call the delegate function in this custom class, the functions do not get called. Here is my custom class:

import Foundation
import UIKit

class PhotoBooth: NSObject, UIScrollViewDelegate {

    private var boothView: UIView
    //private var scrollView: UIScrollView

    init(view: UIView) {

        boothView = view

    }

    func startSession() {
        let width = Double(boothView.frame.width)
        let height = Double(boothView.frame.height)
        let frame = CGRect(x: 0.0, y: 0.0, width: width, height: height)
        let scrollView = UIScrollView(frame: frame)
        //scrollView.backgroundColor = UIColor.blue
        let size = CGSize(width: width + 300, height:1000)
        scrollView.contentSize = size
        boothView.addSubview(scrollView)
        scrollView.delegate = self
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("paisdjfij")
    }

}

And I instantiate the class in my ViewController like so:

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var smallView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let photoBooth = PhotoBooth(view: self.view)
        photoBooth.startSession()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Any solutions to the problem? Please let me know and thank you so much for your help in advance.

aejhyun
  • 612
  • 1
  • 6
  • 19
  • I don't understand what do you expect to happen, you don't have anything inside the `UIScrollView` to scroll as far I saw in your code. – Victor Sigler Nov 15 '16 at 03:38
  • Do I need to add a view to a scrollview in order for the delegate functions to be called? – aejhyun Nov 15 '16 at 03:43
  • 1
    The `UIScrollView` class alone not does anything at all, the point of have content inside a `UIScrollView` is handle to see more content that normally keeps int the screen device, so yes, you need to add ad a `UIView` inside your `UIScrollView` to define some scroll. And always it's recommended put the content inside a `UIScrollView` inside a `UIView` container. Remember the `UIScrollView` need to calculate it `contentSize` based in his content. – Victor Sigler Nov 15 '16 at 03:48

1 Answers1

0

Instead of trying this You should use protocols to call scroll view in custom class.It will look somewhat like this.

CustomClass.h

@protocol CustomDelegate <NSObject>

-(void)customDelegateMethod;

@end

@interface CustomClass : UIScrollView <UIScrollViewDelegate>
{
    id<CustomDelegate> delegate
}

CustomClass.m

-(void) methodScrollView
{
    [self.delegate customDelegateMethod];
}

ViewController.h

@interface ViewController: UIViewController <CustomDelegate>
{
}

ViewController.m

-(void)makeCustomScrollView
{
     CustomClass *objCustom = [[CustomClass alloc] init];
     objCustom.delegate = self;
     //other stuff
}
-(void)customDelegateMethod
{
}
User511
  • 1,456
  • 10
  • 28