斯威夫特代表没有解雇

时间:2016-02-07 19:21:29

标签: swift delegates xcode7

我有一个pageViewController和一个用于内容的itemController。现在我想知道itemController的scrollView位置。我创建了一个协议和一个委托。不知怎的,委托人就是开玩笑。为什么呢?

我的itemController:

protocol scrollPosDelegate{
    func scrollPosChanged(newVal:Float)
}

class PageItemController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var scrollView: UIScrollView!
    var scrollDelegate:scrollPosDelegate? = nil
    var positie: Float = 8.0 {
         didSet {
               if let delegate = scrollDelegate{
                    delegate.scrollPosChanged(self.positie)
                    print(positie)
                    }
                }
    }

    override func viewDidLoad() {
          super.viewDidLoad()
         scrollView.delegate = self
    }

    func scrollViewDidScroll(scrollView: UIScrollView){
        positie += 0.1
    }

}

我的pageViewController:

class PageView: UIViewController, scrollPosDelegate {


    func scrollPosChanged(newVal:Float){
         print("Number Changed:\(newVal)")
    }

}

1 个答案:

答案 0 :(得分:1)

您忘记为scrollDelegate指定值。

顺便说一句,您只需将PageView设置为PageItemController的UIScrollView委托,而不是翻译UIScrollView委托消息。

class PageView: UIViewController, UIScrollViewDelegate {

    // Don't forget to call!
    func setupDelegate() {
         pageItemController.scrollView.delegate = self
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
         print("Number Changed")
    }

}