gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:54:20 25 4
gpt4 key购买 nike

当我实例化 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com