我试图在另一个类中调用协议委托。 第一个类(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.
更多推荐
发布评论