gpt4 book ai didi

xcode - 无法分配给属性 : 'self' is immutable

转载 作者:IT王子 更新时间:2023-10-29 05:44:29 24 4
gpt4 key购买 nike

我正在创建一个页面 View 应用程序。我已经为 ViewController 编写了代码:

import UIKit

class ViewController: UIViewController {

var pageViewController : UIPageViewController!
var pageTitles : NSArray!
var pageImages : NSArray!

@IBAction func restartAction(sender: AnyObject) {}

override func viewDidLoad() {
super.viewDidLoad()

self.pageTitles = NSArray(objects: "This Is Page One!", "This Is Page Two!")
self.pageImages = NSArray(objects: "page1", "page2")

self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

self.pageViewController.dataSource = self // <–– Error

let startVC = self.viewControllerAtIndex(0) as ContentViewController
let viewControllers = NSArray(object: startVC)

self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.height - 60)

self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func viewControllerAtIndex(index: Int) -> ContentViewController {
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return ContentViewController()
}

let vc : ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

vc.imageFile = self.pageImages[index] as! String
vc.titleText = self.pageTitles[index] as! String
vc.pageIndex = index

return vc
}



func pageViewController(pageViewController: UIPageViewController, viewcontrollerBeforeViewController viewController: UIViewController) ->UIViewController? {

let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int

if index == 0 || index == NSNotFound {
return nil
}

index--
return self.viewControllerAtIndex(index)

}

func pageViewController(pageViewController: UIPageViewController, viewcontrollerAfterViewController viewController: UIViewController) ->UIViewController? {

let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int

if index == NSNotFound {
return nil
}

index++

if index == self.pageTitles.count {
return nil
}

return self.viewControllerAtIndex(index)

}

}

但是我在 self.pageViewController.dataSource = self 这一行遇到错误。错误是:

Cannot assign to property: 'self' is immutable

我不确定为什么 self 是不可变的以及如何修复它。如果有人可以提供帮助,请提前致谢。

最佳答案

你需要快速调用 self.pageViewController.setDelegate(self)

委托(delegate)被声明为方法,在某些情况下不是属性

关于xcode - 无法分配给属性 : 'self' is immutable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368787/

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