类型“ SecondPageController”的值没有成员“ miniView”

时间:2019-02-12 00:51:50

标签: swift xcode

我得到的问题似乎相似,但有一点不同。 这是关于声明大小的问题,但是这里有很小的错误。

我认为错误与Main.stroyboard有关,并尝试更改ViewController名称,并且还向ViewController添加了“ miniView”。 我的错误是

“类型为'SecondPageController'的值没有成员'miniView'”,

女巫是一个非常常见的错误(我用这种格式看了很多相同的问题),但是我认为我的错误与添加一些新视图或某些东西有关。

这是我的代码。

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)
}

}

我希望为解决该错误添加一些内容。

1 个答案:

答案 0 :(得分:0)

miniView中没有成员SecondPageController。错误非常精确。

如果要使用名为miniView的组件,则需要引用它,可以使用2种方法:

1-故事板:

将视图拖动到您的UIViewController并将其链接到@IBOutlet weak var miniView: UIView!


2-程序化:

实例化为:let miniView = UIView()


由于您正在使用情节提要,因此您可能忘了在该视图中添加@IBOutlet,因此请使用方法1。