2017-04-21 8 views
0

私はInterface BuilderのHomeViewControllerにリンクされている私のcustomViewを追加しようとしています。ここではストーリーボードの1つのスクリーンショット: IBScrollViewでカスタムUIViewをロード

すべての出口はCarouselView IBOutletsにリンクされています。ここで

のUIViewファイル(Interface Builderのスクリーンショットの中カルーセルビュー):

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

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

ビルドするとき&そのように実行すると、スクロールビューをスクロールできます(3 vi EWSは、追加された)が、コメントしているため何のラベル(通常は)ありませんでした。

scroll1 scroll2

しかし、私は私のCarouselScrollView.swiftファイルにこれらの行の1のコメントを解除する場合:

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

をサブビューとして追加した後にsetupViewを呼び出すと、サブビューとして追加した後にのみラベルが初期化されるため、この問題は解決されます。

+0

1つではなく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 
} 

すべて正しく動作します。

ありがとうございました

関連する問題