我尝试添加我在Interface Builder中的HomeViewcontroller中链接的customView。这里是故事板的一个截图: How to use $http promise response outside success handler.
所有商店都与CarouselView 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
}}
相关联。
这里的UIView文件(Interface Builder的screenShot中的Carousel View):
CarouselScrollView.swift
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)
}}
这是我的ViewController,我在其中用3个CarouselView填充我的scrollView:
// self.assignmentStatusLabel.text = status
// self.assignmentCustomerLabel.text = customer
// self.assignmentAgencyLabel.text = agency
// self.assignmentPeriodLabel.text = period
当我建立&运行就像我可以滚动我的滚动视图(添加了3个视图)但没有标签(正常,因为评论):
但是当我在CarouselScrollView.swift文件中取消注释其中一行时:
nil
我在控制台中出现此错误:
致命错误:在解包可选值时意外发现nil
我不明白为什么我的标签是{{1}}。一切似乎都好。有人有解决方案或想法吗?提前感谢您的时间和帮助。
答案 0 :(得分:0)
如果您在setupView
添加为子视图后致电carouselView
,问题就会得到解决,因为只有在添加为子视图后,您的标签才会被初始化。
答案 1 :(得分:0)
就像所说的Paramasivan Samuttiram一样,我必须在单独的Nib(.xib)文件中创建我的customView。我希望有另一个解决方案,以便能够将customView保留在我的故事板中(以保持它在我的HomeController上方)但是这样做了。
在CarouselView中我添加了这个:
class func instanceFromNib() -> UIView { return UINib(nibName: "CarouselView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView }
现在CarouselView文件如下所示:
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!
class func instanceFromNib() -> UIView {
return UINib(nibName: "CarouselView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}}
在HomeViewController中,我在func scrollViewSetup的循环中实例化了我的3个自定义视图:
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.instanceFromNib() as! CarouselView
carouselView.frame = CGRect(x: carouselScrollViewWidth * CGFloat(i), y: CGFloat(0), width: carouselScrollViewWidth, height: carouselScrollViewHeight)
carouselView.assignmentStatusLabel.text = self.arrayOfAssignments[i]["status"]
carouselView.assignmentCustomerLabel.text = self.arrayOfAssignments[i]["customer"]?.uppercased()
carouselView.assignmentAgencyLabel.text = self.arrayOfAssignments[i]["agency"]?.uppercased()
carouselView.assignmentPeriodLabel.text = "\(self.arrayOfAssignments[i]["startDate"]!) - \(self.arrayOfAssignments[i]["endDate"]!)"
self.carouselScrollView.addSubview(carouselView)
}
self.carouselScrollView.delegate = self
}
现在一切正常。
感谢所有人,