ios - UIScrollViewDelegate 未在自定义类中调用

标签 ios swift uiscrollviewdelegate

当我实例化 ScrollView 并将其添加到 ViewController 类中的 self.view 并将 ScrollView 的委托(delegate)设置为 self 时,委托(delegate)函数将被调用。如下所示:

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.")
    }

}

但是,当我创建一个自定义类(在本例中称为 PhotoBooth)并尝试调用此自定义类中的委托(delegate)函数时,这些函数不会被调用。这是我的自定义类:

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")
    }

}

我在我的 ViewController 中实例化该类,如下所示:

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.
    }

}

有什么办法可以解决这个问题吗?请让我知道,并非常感谢您提前提供的帮助。

最佳答案

您应该使用协议(protocol)在自定义类中调用 ScrollView ,而不是尝试这个。它看起来有点像这样。

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
{
}

关于ios - UIScrollViewDelegate 未在自定义类中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40601534/

相关文章:

iphone - 升级操作系统应用程序时迁移 sqlite 数据库有哪些选项?

ios - 在离线或飞行模式下处理远程推送通知

iphone - 如何计算减速结束后 ScrollView 将停止的 contentOffset?

ios - 使用 UIScrollView 更改图像的 alpha

tvOS上的UIScrollView

iphone - 如何在使用 AVAudioPlayer 的应用程序的多任务栏中启用音频控件?

iOS 呈现的 UINavigationController 在执行 popViewController 后被解雇

ios - 将通用的应用内购买项目用于动态范围的数字产品

swift - 调出键盘时,更改滚动插图会自动设置动画

swift - 获取 PFUser 引用对象