2017-04-21 6 views
0

Interface Builder에서 HomeViewController에 링크 된 customView를 추가하려고합니다. 스토리 보드의 스크린 샷입니다. IBScrollView에서 사용자 정의 UIView로드

모든 아울렛은 CarouselView IBOutlets에 연결됩니다. 여기

UIView의 파일 (인터페이스 빌더의 스크린 샷에서 회전 목마보기) :

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

내가 3 CarouselView 내있는 ScrollView를 채우기 내의 ViewController :

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

빌드 할 때 & 실행하면 스크롤보기 (3 vi EWS가 추가되었다)하지만 주석 때문에 정상에 레이블()가없는 : 내 CarouselScrollView.swift 파일에이 라인 중 하나의 주석을 때

scroll1 scroll2

그러나 :

//  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

내 레이블이인 이유를 모르겠다. 210. 모든게 괜찮아 보인다. 누군가 해결책이나 아이디어를 가지고 있습니까?. 사전에 시간과 도움에 감사드립니다.

+0

레이블이 Interface Builder의 콘센트에 제대로 연결되어 있는지 확인하십시오. –

+0

고맙습니다.하지만 이미 몇 번했는데 문제가 아닙니다. –

+0

'awakeFromNib'가 호출되기 전에 레이블을 사용하고있을 수도 있습니다. –

답변

0

carouselView이 하위보기로 추가 된 후에 setupView을 호출하면 문제가 해결됩니다. 하위보기로 추가 한 후에 만 ​​레이블이 초기화되기 때문입니다.

+0

하나 대신 2 개의 루프를 만들지 않고 어떻게 할 수 있습니까? 너보다 user3057272 –

+1

'for'루프에 대해 이야기하고 있다면 'setupView'에서 추적 할 수있는 속성을 만들어야합니다. –

0

Paramasivan Samuttiram처럼 별도의 Nib (.xib) 파일로 customView를 만들어야했습니다. 내 스토리 보드 내에서 customView를 유지할 수있는 또 다른 솔루션이 있기를 바라고 있었지만 (내 HomeController 바로 위에 유지) 트릭을 수행했습니다.

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

내가 FUNC의 scrollViewSetup 내 루프에서 그런 내 3 개 사용자 정의보기를 인스턴스화 HomeViewController에

:

CarouselView에서

나는이 추가

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 
} 

모두 올바르게 작동합니다.

덕분에