@obj协议委托方法在第二类中不起作用。(@obj protocol delegate method not working in second class. Swift)

编程入门 行业动态 更新时间:2024-10-25 21:14:59
@obj协议委托方法在第二类中不起作用。(@obj protocol delegate method not working in second class. Swift)

我试图在另一个类中调用协议委托。 第一个类(ViewController)可以工作,但我实现的第二个类没有显示任何数据。 我添加了自动填充选项,因此它不会出错。 它什么都没做。

发件人类

@objc protocol BWWalkthroughViewControllerDelegate{ @objc optional func walkthroughPageDidChange(pageNumber:Int) // Called when current page changes } @objc class BWWalkthroughViewController: UIViewController, UIScrollViewDelegate, ViewControllerDelegate { // MARK: - Public properties - weak var delegate:BWWalkthroughViewControllerDelegate? var currentPage:Int{ // The index of the current page (readonly) get{ let page = Int((scrollview.contentOffset.x / view.bounds.size.width)) return page } } // MARK: - Private properties - let scrollview:UIScrollView! var controllers:[UIViewController]! var lastViewConstraint:NSArray? var shouldCancelTimer = false var aSound: AVAudioPlayer! var isForSound: AVAudioPlayer! var alligatorSound: AVAudioPlayer! var audioPlayer = AVAudioPlayer?() var error : NSError? var soundTrack2 = AVAudioPlayer?() let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate var audioPlayerAnimalSound = AVAudioPlayer?() var audioPlayerAlphabetSound = AVAudioPlayer?() var audioPlayerFullPhraze = AVAudioPlayer?() var audioPlayerPhonics = AVAudioPlayer?()

删除代码以节省空间继续:

/** Update the UI to reflect the current walkthrough situation **/ private func updateUI(){ // Get the current page pageControl?.currentPage = currentPage // Notify delegate about the new page delegate?.walkthroughPageDidChange?(currentPage) }

接收器类

class BWWalkthroughPageViewController: UIViewController, BWWalkthroughPage, ViewControllerDelegate, BWWalkthroughViewControllerDelegate {

功能在二等。

func walkthroughPageDidChange(pageNumber: Int) { println("current page 2 \(pageNumber)") }

但是,walkthroughPageDidChange在viewController类中可以正常工作。 请问你能帮我看看有什么问题吗?

Im trying to call protocol delegate in an additional class. The first class (ViewController) works but the second one I have implemented it in doesn't show any data. I added it with the autofill option so its not giving errors. It just doesn't do anything.

sender class

@objc protocol BWWalkthroughViewControllerDelegate{ @objc optional func walkthroughPageDidChange(pageNumber:Int) // Called when current page changes } @objc class BWWalkthroughViewController: UIViewController, UIScrollViewDelegate, ViewControllerDelegate { // MARK: - Public properties - weak var delegate:BWWalkthroughViewControllerDelegate? var currentPage:Int{ // The index of the current page (readonly) get{ let page = Int((scrollview.contentOffset.x / view.bounds.size.width)) return page } } // MARK: - Private properties - let scrollview:UIScrollView! var controllers:[UIViewController]! var lastViewConstraint:NSArray? var shouldCancelTimer = false var aSound: AVAudioPlayer! var isForSound: AVAudioPlayer! var alligatorSound: AVAudioPlayer! var audioPlayer = AVAudioPlayer?() var error : NSError? var soundTrack2 = AVAudioPlayer?() let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate var audioPlayerAnimalSound = AVAudioPlayer?() var audioPlayerAlphabetSound = AVAudioPlayer?() var audioPlayerFullPhraze = AVAudioPlayer?() var audioPlayerPhonics = AVAudioPlayer?()

Code removed to save space carries on:

/** Update the UI to reflect the current walkthrough situation **/ private func updateUI(){ // Get the current page pageControl?.currentPage = currentPage // Notify delegate about the new page delegate?.walkthroughPageDidChange?(currentPage) }

receiver class

class BWWalkthroughPageViewController: UIViewController, BWWalkthroughPage, ViewControllerDelegate, BWWalkthroughViewControllerDelegate {

Function in second Class.

func walkthroughPageDidChange(pageNumber: Int) { println("current page 2 \(pageNumber)") }

walkthroughPageDidChange does work in the viewController class however. Please can you help me see what is wrong?

最满意答案

你的weak var delegate:BWWalkthroughViewControllerDelegate? 是一个可选变量,它不会在您呈现的代码中的任何位置分配,因此它将为零。 你必须在某处实例化它才能工作。

这样做的好方法是:

class BWWalkthroughViewController { let bwWalkthroughPageViewController = BWWalkthroughPageViewController() var bwWalkthroughViewControllerDelegate : BWWalkthroughViewControllerDelegate! init() { bwWalkthroughViewControllerDelegate = bwWalkthroughPageViewController as BWWalkthroughViewControllerDelegate } }

需要注意的是,为代理命名有意义的名称通常是一种好习惯。 关键字“委托”通常用于许多类并受到限制。 更详细的名称将消除覆盖原始代表的机会,并且如果您有多个代理,将有助于识别每个代理。

Your weak var delegate:BWWalkthroughViewControllerDelegate? is an optional variable and it is not assigned anywhere in your presented code, hence it will be nil. You have to instantiate it somewhere for it to work.

A good way to do so is:

class BWWalkthroughViewController { let bwWalkthroughPageViewController = BWWalkthroughPageViewController() var bwWalkthroughViewControllerDelegate : BWWalkthroughViewControllerDelegate! init() { bwWalkthroughViewControllerDelegate = bwWalkthroughPageViewController as BWWalkthroughViewControllerDelegate } }

A thing to note is that it is often good practice to name the delegates with meaningful names. The keyword "delegate" is often used for many classes and is restricted. A more verbose name will eliminate the chance of overriding an original delegate and will help identify each one if you have more than one.

更多推荐

本文发布于:2023-07-30 01:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   第二类   协议   方法   obj

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!