为什么滚动视图为零?
Why is the scroll view being nil?
我有以下代码:
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}
@IBOutlet weak var scrollView: UIScrollView!
然后我像这样调用 viewDidLoad 中的函数:
scrollViewDidScroll(scrollView)
尽管我总是收到错误消息
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
我做错了什么,如何让滚动视图不为零。
在viewDidLoad()
中分配滚动视图的.delegate
并开始滚动:
class SegmentedControlViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// assign the delegate
scrollView.delegate = self
}
}
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}
编辑
如果要检查滚动视图内容是否已向右拖动超过 10 点,请使用:
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if scrollView.contentOffset.x < 10 {
print("scroll content has been dragged to the right more than 10 points")
}
}
}
我有以下代码:
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}
@IBOutlet weak var scrollView: UIScrollView!
然后我像这样调用 viewDidLoad 中的函数:
scrollViewDidScroll(scrollView)
尽管我总是收到错误消息
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
我做错了什么,如何让滚动视图不为零。
在viewDidLoad()
中分配滚动视图的.delegate
并开始滚动:
class SegmentedControlViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// assign the delegate
scrollView.delegate = self
}
}
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if abs(scrollView.contentOffset.x - 10) < 1 {
print("works")
}
}
}
编辑
如果要检查滚动视图内容是否已向右拖动超过 10 点,请使用:
extension SegmentedControlViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// this checks for whether contentOffset.x is around 10 (contented shifted to the right by 10 points)
if scrollView.contentOffset.x < 10 {
print("scroll content has been dragged to the right more than 10 points")
}
}
}