UIPageControl custom class - found nil changing im

2019-04-15 20:30发布

I need to implement a UIPageControl with custom images instead the normal dot. So I create a custom class and connect it through the storyboard. Since ios7 the subview of UIPageControl contain UIView instead of UIImageView. The subviews of the resulting UIView(UIIpageControl subviews) doesn't contain any subviews so I receive the error:

fatal error: unexpectedly found nil while unwrapping an Optional value.

Where I might have been wrong?

class WhitePageControl:UIPageControl{
   let activeImage = UIImage(named: "dot_white")
   let inactiveImage = UIImage(named: "dot_white_e")

   override init(frame: CGRect){
     super.init(frame: frame)
   }

   required init(coder aDecoder: NSCoder){
     super.init(coder: aDecoder)
   }

   func updateDots(){
     println(self.subviews.count) // 3
     println(self.numberOfPages) // 3
     for var index = 0; index < self.subviews.count; index++ {
        println(index)
        var dot:UIImageView!
        var dotView:UIView = self.subviews[index] as UIView
        println("1")
        for subview in dotView.subviews{ // NIL HERE
            println("2")
            if subview.isKindOfClass(UIImageView){
                println("3")
                dot = subview as UIImageView
                if index == self.currentPage{ dot.image = activeImage }
                else{ dot.image = inactiveImage }
            }
            }
         }
       }

    func setCurrentPage(value:Int){
        super.currentPage = value
        self.updateDots()
    }
}

2条回答
倾城 Initia
2楼-- · 2019-04-15 21:13

It's my solution:

import Foundation

class PageControl: UIPageControl {
    var activeImage: UIImage!
    var inactiveImage: UIImage!
    override var currentPage: Int {
        //willSet {
        didSet { //so updates will take place after page changed
            self.updateDots()
        }
    }

    convenience init(activeImage: UIImage, inactiveImage: UIImage) {
        self.init()

        self.activeImage = activeImage
        self.inactiveImage = inactiveImage

        self.pageIndicatorTintColor = UIColor.clearColor()
        self.currentPageIndicatorTintColor = UIColor.clearColor()
    }

    func updateDots() {
        for var i = 0; i < count(subviews); i++ {
            var view: UIView = subviews[i] as! UIView
            if count(view.subviews) == 0 {
                self.addImageViewOnDotView(view, imageSize: activeImage.size)
            }
            var imageView: UIImageView = view.subviews.first as! UIImageView
            imageView.image = self.currentPage == i ? activeImage : inactiveImage
        }
    }

    // MARK: - Private

    func addImageViewOnDotView(view: UIView, imageSize: CGSize) {
        var frame = view.frame
        frame.origin = CGPointZero
        frame.size = imageSize

        var imageView = UIImageView(frame: frame)
        imageView.contentMode = UIViewContentMode.Center
        view.addSubview(imageView)
    }

}
查看更多
淡お忘
3楼-- · 2019-04-15 21:26

change setCurrentPage to Current Page ,bcoz that one is Obj C property

 func currentPage(page: Int) {
    super.currentPage = page
    self.updateDots()
}

try this.

查看更多
登录 后发表回答