gpt4 book ai didi

ios - 在 ScrollView 中加载自定义 UIView

转载 作者:行者123 更新时间:2023-11-30 12:31:07 25 4
gpt4 key购买 nike

我尝试添加我的自定义 View ,该 View 链接到界面生成器中的 HomeViewcontroller 中。这是 Storyboard的屏幕截图: IB

所有 socket 均链接到 CarouselView IBOutlets

这里是 UIView 文件(Interface Builder 屏幕截图中的轮播 View ):

CarouselScrollView.swift

import UIKit

class CarouselView: UIView {
@IBOutlet weak var assignmentStatusLabel: UILabel!
@IBOutlet weak var assignmentCustomerLabel: UILabel!
@IBOutlet weak var assignmentAgencyLabel: UILabel!
@IBOutlet weak var assignmentPeriodLabel: UILabel!
@IBOutlet weak var nextAssignmentButton: UIButton!
@IBOutlet weak var previousAssignmentButton: UIButton!

init(frame: CGRect, _ status: String, _ customer: String, _ agency: String, _ period: String) {
super.init(frame: frame)
self.setupView("status", "customer", "agency", "period")
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func setupView(_ status: String, _ customer: String, _ agency: String, _ period: String) {
// self.assignmentStatusLabel.text = status
// self.assignmentCustomerLabel.text = customer
// self.assignmentAgencyLabel.text = agency
// self.assignmentPeriodLabel.text = period
}}

这是我的 ViewController,我用 3 个 CarouselView 填充 ScrollView :

import UIKit
class HomeViewController: BaseViewController, HomeView {

@IBOutlet weak var carouselScrollView: UIScrollView!
@IBOutlet weak var carouselView: CarouselView!

var presenter: HomePresenter?
var currentAssignmentView: Int = 0
// create array of assignments for test
let arrayOfAssignments = [["status":"Mission en cours", "customer":"customer1", "agency":"bourg", "startDate":"09-05-2016", "endDate":"10-05-2016"],
["status":"Mission à venir", "customer":"customer2", "agency":"agency paris", "startDate":"09-01-2017", "endDate":"03-04-2017"],
["status":"Mission passée", "customer":"customer3", "agency":"agency lyon", "startDate":"09-09-2017", "endDate":"29-05-2018"]
]

override func initPresenter() -> BasePresenter {
let presenter = HomePresenter(self, Navigator(self))
self.presenter = presenter
return presenter
}

override func setup() {
self.navBarTitleImageSetup()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollViewSetup()
}

func scrollViewSetup() {
let carouselScrollViewWidth:CGFloat = self.carouselScrollView.frame.width
let carouselScrollViewHeight:CGFloat = self.carouselScrollView.frame.height
let numberOfAssignments = self.arrayOfAssignments.count

self.carouselScrollView.contentSize = CGSize(width: carouselScrollViewWidth * CGFloat(numberOfAssignments), height: carouselScrollViewHeight)
for i in 0...numberOfAssignments - 1 {
let carouselView = CarouselView(frame: CGRect(x: carouselScrollViewWidth * CGFloat(i), y: CGFloat(0), width: carouselScrollViewWidth, height: carouselScrollViewHeight), "iuhiuh", "oiojoij", "iuhiuh", "iuhiuhiuh")
switch i {
case 0:
carouselView.backgroundColor = UIColor.blue
case 1:
carouselView.backgroundColor = UIColor.yellow
case 2:
carouselView.backgroundColor = UIColor.gray
default:
break
}
self.carouselScrollView.addSubview(carouselView)
}
self.carouselScrollView.delegate = self
}

func navBarTitleImageSetup() {
let image: UIImage = #imageLiteral(resourceName: "NavLogo")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 97, height: 24))
imageView.contentMode = .scaleAspectFit
imageView.image = image
self.navigationItem.titleView = imageView
}}

extension HomeViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let viewWidth: CGFloat = self.carouselScrollView.frame.width
let currentAssignmentNumber: CGFloat = floor((self.carouselScrollView.contentOffset.x - viewWidth/2)/viewWidth)+1
self.currentAssignmentView = Int(currentAssignmentNumber)
}}

当我像这样构建和运行时,我可以滚动 ScrollView (添加了 3 个 View ),但没有标签(正常,因为已注释):

scroll1 scroll2

但是当我在 CarouselScrollView.swift 文件中取消注释其中一行时:

    //        self.assignmentStatusLabel.text = status
// self.assignmentCustomerLabel.text = customer
// self.assignmentAgencyLabel.text = agency
// self.assignmentPeriodLabel.text = period

我在控制台中遇到此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

我不明白为什么我的标签是nil。一切看起来都很好。有人有解决方案或想法吗?预先感谢您的时间和帮助。

最佳答案

如果您在将 carouselView 添加为 subview 后调用 setupView,问题就会得到解决,因为只有在添加为 subview 后,您的标签才会被初始化。

关于ios - 在 ScrollView 中加载自定义 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43550329/

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