@obj 协议委托方法在第二个 class 中不起作用。 Swift
@obj protocol delegate method not working in second class. Swift
我试图在另一个 class 中调用协议委托。第一个 class (ViewController) 有效,但我实现它的第二个不显示任何数据。我用自动填充选项添加了它,所以它不会出错。它只是什么都不做。
发件人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?()
代码删除以保存 space 继续:
/**
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
class BWWalkthroughPageViewController: UIViewController, BWWalkthroughPage, ViewControllerDelegate, BWWalkthroughViewControllerDelegate {
第二个函数 Class。
func walkthroughPageDidChange(pageNumber: Int) {
println("current page 2 \(pageNumber)")
}
walkthroughPageDidChange 确实在 viewController class 中起作用。你能帮我看看哪里出了问题吗?
你的 weak var delegate:BWWalkthroughViewControllerDelegate?
是一个可选变量,它没有在你提供的代码中的任何地方赋值,因此它将是 nil。你必须在某个地方实例化它才能工作。
这样做的一个好方法是:
class BWWalkthroughViewController {
let bwWalkthroughPageViewController = BWWalkthroughPageViewController()
var bwWalkthroughViewControllerDelegate : BWWalkthroughViewControllerDelegate!
init() {
bwWalkthroughViewControllerDelegate = bwWalkthroughPageViewController as BWWalkthroughViewControllerDelegate
}
}
需要注意的一点是,用有意义的名称命名委托通常是一种很好的做法。关键字 "delegate" 经常用于许多 类 并且受到限制。更详细的名称将消除覆盖原始委托的机会,并且如果您有多个委托,将有助于识别每个委托。
我试图在另一个 class 中调用协议委托。第一个 class (ViewController) 有效,但我实现它的第二个不显示任何数据。我用自动填充选项添加了它,所以它不会出错。它只是什么都不做。
发件人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?()
代码删除以保存 space 继续:
/**
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
class BWWalkthroughPageViewController: UIViewController, BWWalkthroughPage, ViewControllerDelegate, BWWalkthroughViewControllerDelegate {
第二个函数 Class。
func walkthroughPageDidChange(pageNumber: Int) {
println("current page 2 \(pageNumber)")
}
walkthroughPageDidChange 确实在 viewController class 中起作用。你能帮我看看哪里出了问题吗?
你的 weak var delegate:BWWalkthroughViewControllerDelegate?
是一个可选变量,它没有在你提供的代码中的任何地方赋值,因此它将是 nil。你必须在某个地方实例化它才能工作。
这样做的一个好方法是:
class BWWalkthroughViewController {
let bwWalkthroughPageViewController = BWWalkthroughPageViewController()
var bwWalkthroughViewControllerDelegate : BWWalkthroughViewControllerDelegate!
init() {
bwWalkthroughViewControllerDelegate = bwWalkthroughPageViewController as BWWalkthroughViewControllerDelegate
}
}
需要注意的一点是,用有意义的名称命名委托通常是一种很好的做法。关键字 "delegate" 经常用于许多 类 并且受到限制。更详细的名称将消除覆盖原始委托的机会,并且如果您有多个委托,将有助于识别每个委托。