gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:12:23 24 4
gpt4 key购买 nike

我已经正确设置了 UIPageViewController。

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

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

我尝试了什么

  1. 我运行应用
  2. 控制台输出:“角色是约翰!”
  3. 我向前(向右)滑动
  4. 控制台输出:“角色是 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,或其他一些值来触发函数。

关于swift - 为什么这个 didSet 属性没有在 Swift 中打印任何输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27947368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com