我得到的问题看起来很相似但又有点不同。 这是关于声明大小的问题,但这里有小错误。
我认为错误与 Main.stroyboard 有关,并尝试更改 ViewController 名称并将“miniView”添加到 ViewController。 我的错误是
“‘SecondPageController’类型的值没有成员‘miniView’”,
女巫是很常见的错误(我用这种格式看了很多相同的问题),但我认为我的错误与添加一些新 View 或其他东西有关。
这是我的代码。
class SecondPageController: UIViewController {
@IBOutlet weak var mainScrollView: UIScrollView!
var imageArray = [UIImage]()
var currentPage = 0
override func viewDidLoad() {
super.viewDidLoad()
imageArray = [UIImage(named: "bus1")!, UIImage(named: "bus2")!]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.miniView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)
}
Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(startAnimating), userInfo: nil, repeats: true)
}
@objc func startAnimating() {
//that line check if the current page is the last one, and set it to the first, otherwise, it increment the currentPage
self.currentPage = (self.currentPage == imageArray.count-1) ? 0 : self.currentPage+1
var newOffset = mainScrollView.contentOffset
newOffset.x = mainScrollView.frame.width * CGFloat(self.currentPage)
self.mainScrollView.setContentOffset(newOffset, animated: true)
}
}
我期待为这个错误解决添加一些东西。
最佳答案
SecondPageController
中没有成员miniView
。错误非常精确。
如果你想使用一个名为 miniView
的组件,你需要一个对它的引用,你可以使用两种方法:
1 - Storyboard:
将 View 拖到您的 UIViewController
并将其链接到 @IBOutlet weak var miniView: UIView!
2 - 程序化:
像这样实例化它:let miniView = UIView()
由于您使用的是 Storyboard,您可能忘记将 @IBOutlet
添加到该 View ,因此请使用方法 1。
关于swift - 类型 'SecondPageController' 的值没有成员 'miniView',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54641280/