2016-10-09 8 views
0

私は、Food TrackerアプリチュートリアルをSwiftを習得するプロジェクトとして取り組んできました。これはバグがたくさんあることを理解していますが、コードを正しくフォーマットする方法を研究しています。私は星評価セクションに行き、赤いボタンを追加し、それを5つのボタンに分けて、星を追加しました。星はちょっと見えましたが、私のシミュレータにはまったく表示されません。私は自分のコードを整理し、画像と画像コードを外し、接続を外してIBOutletsなどを追加しました。まだ表示されていないので、私がコードを忘れていると思っています。どんな助けでも大歓迎です。xcodeを実行するスウィフトシミュレータにビューオブジェクトが表示されない8

あなたのinitで
import UIKit 

class RatingControl: UIView { 
    // MARK: Properties 

    var rating = 0 { 
     didSet { 
      setNeedsLayout() 
     } 
    } 
    var ratingButtons = [UIButton]() 
    var spacing = 5 
    var stars = 5 

    // MARK: Initialization 

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

     let filledStar = UIImage(named: "filledStar") 
     let emptyStar = UIImage(named: "emptyStar") 

     for _ in 0..<5 { 
      let button = UIButton() 

      button.setImage(emptyStar, for: UIControlState()) 
      button.setImage(filledStar, for: .selected) 
      button.setImage(filledStar, for: [.highlighted, .selected]) 

      button.adjustsImageWhenHighlighted = false 

      button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(_:)), for: .touchDown);   ratingButtons += [button] 
      addSubview(button) 
     } 
    } 

    override func layoutSubviews() { 
     // Set the button's width and height to a square the size of the frame's height. 
     let buttonSize = Int(frame.size.height) 
     var buttonFrame = CGRect(x: 0, y: 0, width: buttonSize, height: buttonSize) 

     // Offset each button's origin by the length of the button plus spacing. 
     for (index, button) in ratingButtons.enumerated() { 
      buttonFrame.origin.x = CGFloat(index * (buttonSize + spacing)) 
      button.frame = buttonFrame 
     } 
     updateButtonSelectionStates() 
    } 

    override var intrinsicContentSize : CGSize { 
     let buttonSize = Int(frame.size.height) 
     let width = (buttonSize + spacing) * stars 

     return CGSize(width: width, height: buttonSize) 
    } 

    // MARK: Button Action 

    func ratingButtonTapped(_ button: UIButton) { 
     rating = ratingButtons.index(of: button)! + 1 

     updateButtonSelectionStates() 
    } 

    func updateButtonSelectionStates() { 
     for (index, button) in ratingButtons.enumerated() { 
      // If the index of a button is less than the rating, that button should be selected. 
      button.isSelected = index < rating 
     } 
    } 
} 

答えて

0

?()メソッドは、結果何も追加してください配列を列挙し、アクセスされたときに上がっていない、そしてそれは

空であるようratingButtonsアレイにボタンを追加見逃しています右

addSubview(button) 

self.ratingsButton.append(button) 

を以下うまくいけば、ボタンがSHを開始します負う。

関連する問題