简体   繁体   English

@obj协议委托方法在第二类中不起作用。 迅速

[英]@obj protocol delegate method not working in second class. Swift

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. 第一个类(ViewController)可以工作,但我实现的第二个类不显示任何数据。 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. 但是,walkthroughPageDidChange确实可以在viewController类中使用。 Please can you help me see what is wrong? 请您能帮我看看有什么问题吗?

Your weak var delegate:BWWalkthroughViewControllerDelegate? 您的weak var delegate:BWWalkthroughViewControllerDelegate? is an optional variable and it is not assigned anywhere in your presented code, hence it will be nil. 是一个可选变量,不会在您呈现的代码中分配任何位置,因此它将为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. 关键字“ delegate”通常用于许多类,并且受到限制。 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. 更详细的名称将消除覆盖原始代理的机会,并且如果您有多个代理,则将有助于识别每个代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM