为什么这个 didSet 属性 没有在 Swift 中打印任何输出?

Why is this didSet property not printing any output in Swift?

我已经正确设置了 UIPageViewController。

我想检查每个视图的 itemIndex 并相应地显示内容。代码如下所示:

var itemIndex: Int = 0 {
    didSet {
        if itemIndex == 1 {
            println("Character is Lela!")
        }
        if itemIndex == 0 {
            println("Character is John!")
    }  
  } 
}

我试过的

  1. 我运行 应用程序
  2. 控制台输出:"Character is John!"
  3. 我向前(向右)滑动
  4. 控制台输出:"Character is Lela!"
  5. 我向后滑动(向左)
  6. 没有输出!
  7. 我向前滑动
  8. 没有输出!再来一次!

这是因为什么?这是完整的代码:

import UIKit

var currentIndex: Int = 0
var nextIndex: Int = 0

class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pageViewController: UIPageViewController?

let characterImages = ["character1", "character2", "character1", "character2", "character1", "character2", "character1", "character2"]

override func viewDidLoad() {
    super.viewDidLoad()
    createPageViewController()
    setupPageControl()

    character = 1
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// Forward, check if this IS NOT the last controller
func pageViewController(pageViewController: UIPageViewController,
    viewControllerAfterViewController ProView: UIViewController) -> UIViewController? {

        let itemController = ProView as PageItemController

        // Check if there is another view
        if itemController.itemIndex+1 < characterImages.count {

            return getItemController(itemController.itemIndex+1)
        }

        return nil
}


// Check if this IS NOT the first controller
func pageViewController(pageViewController: UIPageViewController,
    viewControllerBeforeViewController ProView: UIViewController) -> UIViewController? {

    let itemController = ProView as PageItemController

        if itemController.itemIndex < 0 {

            return getItemController(itemController.itemIndex-1)
        }

        return nil
}

private func getItemController(itemIndex: Int) -> PageItemController? {

    if itemIndex < characterImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = characterImages[itemIndex]
        return pageItemController
}

    return nil
}

func createPageViewController() {

    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
    pageController.dataSource = self
    pageController.delegate = self

    if characterImages.count > 0 {
        let firstController = getItemController(0)!
        let startingViewControllers: NSArray = [firstController]
        pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    pageViewController = pageController
    addChildViewController(pageViewController!)
    self.view.addSubview(pageViewController!.view)
    pageViewController?.didMoveToParentViewController(self)
}

func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return characterImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {

return 0
}

// BETA

func pageViewController(PageItemController: UIPageViewController,
    didFinishAnimating finished: Bool,
    previousViewControllers pageViewController: [AnyObject],
    transitionCompleted completed: Bool)
{

    if (!completed)
    {
        // You do nothing because whatever page you thought
        // the book was on before the gesture started is still the correct page
        return;
    }
    // This is where you would know the page number changed and handle it appropriately
    }

}

class PageItemController: UIViewController {

@IBOutlet weak var imageCharacterChoose: UIImageView!

var itemIndex: Int = 0 {
    didSet {
        if itemIndex == 1 {
            println("Character is Lela!")
        }
        if itemIndex == 0 {
            println("Character is John!")
    }

    }}
var imageName: String = "" {
    didSet {

        if let imageView = imageCharacterChoose {imageCharacterChoose.image = UIImage(named: imageName)
        }

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    imageCharacterChoose!.image = UIImage(named: imageName)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

didSetwillSet 只会在您更改相应变量的值时被调用,因此在您的情况下,您应该在代码的某处执行 itemIndex = 1,或其他一些触发函数的值。