简体   繁体   中英

@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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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