2017-09-15 47 views
0

我在我的應用程序中使用RatingControl類(在gitHub上找到)記錄4個條件(實例)的反饋。我必須從viewController中分別捕獲每個實例中點擊的星的數量。我如何捕獲捕獲從viewController點擊的星星數量,並區分哪個實例已被點擊。我相信我必須使用RatingControl類的ratingButtonTapped函數,但不知道如何實現這一點。RatingControl - 捕獲從viewController點擊的星星數量

下面的ViewController代碼片段:

class DetailsViewController: UIViewController { 

    @IBOutlet weak var ratingControl1: RatingControl! 
    @IBOutlet weak var ratingControl2: RatingControl! 
    @IBOutlet weak var ratingControl3: RatingControl! 
    @IBOutlet weak var ratingControl4: RatingControl! 

    //I want to use some code within ViewController to capture the #stars clicked 

RatingControl以下類:

@IBDesignable class RatingControl: UIStackView { 

//MARK: Properties 

private var ratingButtons = [UIButton]() 

var rating = 0 { 
    didSet { 
     updateButtonSelectionStates() 
    } 
} 

@IBInspectable var starSize: CGSize = CGSize(width: 44.0, height: 44.0) { 
    didSet { 
     setupButtons() 
    } 
} 

@IBInspectable var starCount: Int = 5 { 
    didSet { 
     setupButtons() 
    } 
} 

//MARK: Initialization 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    setupButtons() 
} 

required init(coder: NSCoder) { 
    super.init(coder: coder) 
    setupButtons() 
} 

//MARK: Button Action 

func ratingButtonTapped(button: UIButton) { 
    guard let index = ratingButtons.index(of: button) else { 
     fatalError("The button, \(button), is not in the ratingButtons array: \(ratingButtons)") 
    } 

    // Calculate the rating of the selected button 
    let selectedRating = index + 1 

    if selectedRating == rating { 
     // If the selected star represents the current rating, reset the rating to 0. 
     rating = 0 
    } else { 
     // Otherwise set the rating to the selected star 
     rating = selectedRating 
    } 

    print(selectedRating) 
} 


//MARK: Private Methods 

private func setupButtons() { 

    // Clear any existing buttons 
    for button in ratingButtons { 
     removeArrangedSubview(button) 
     button.removeFromSuperview() 
    } 
    ratingButtons.removeAll() 

    // Load Button Images 
    let bundle = Bundle(for: type(of: self)) 
    let filledStar = UIImage(named: "filledStar", in: bundle, compatibleWith: self.traitCollection) 
    let emptyStar = UIImage(named:"emptyStar", in: bundle, compatibleWith: self.traitCollection) 
    let highlightedStar = UIImage(named:"highlightedStar", in: bundle, compatibleWith: self.traitCollection) 

    for index in 0..<starCount { 
     // Create the button 
     let button = UIButton() 

     // Set the button images 
     button.setImage(emptyStar, for: .normal) 
     button.setImage(filledStar, for: .selected) 
     button.setImage(highlightedStar, for: .highlighted) 
     button.setImage(highlightedStar, for: [.highlighted, .selected]) 

     // Add constraints 
     button.translatesAutoresizingMaskIntoConstraints = false 
     button.heightAnchor.constraint(equalToConstant: starSize.height).isActive = true 
     button.widthAnchor.constraint(equalToConstant: starSize.width).isActive = true 

     // Set the accessibility label 
     button.accessibilityLabel = "Set \(index + 1) star rating" 

     // Setup the button action 
     button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside) 

     // Add the button to the stack 
     addArrangedSubview(button) 

     // Add the new button to the rating button array 
     ratingButtons.append(button) 
    } 

    updateButtonSelectionStates() 
} 

private 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 

     // Set accessibility hint and value 
     let hintString: String? 
     if rating == index + 1 { 
      hintString = "Tap to reset the rating to zero." 
     } else { 
      hintString = nil 
     } 

     let valueString: String 
     switch (rating) { 
     case 0: 
      valueString = "No rating set." 
     case 1: 
      valueString = "1 star set." 
     default: 
      valueString = "\(rating) stars set." 
     } 

     button.accessibilityHint = hintString 
     button.accessibilityValue = valueString 
    } 
    } 
} 
+1

您正在使用哪種回購?應該有一些功能或屬性來訪問提供的評級的當前值。 – Surjeet

+0

在RatingControl類中有一個函數ratingButtonTapped,但是我不知道如何在單擊按鈕時從ViewController類捕獲以及如何區分哪個實例按鈕被單擊。 – dacscan3669

+0

分享回購鏈接。 – Surjeet

回答

1

也許你應該創建協議RatingControlDelegate

@objc protocol RatingControlDelegate: NSObjectProtocol { 
    @objc optional func ratingButtonTapped(_ ratingControl: RatingControl) 
} 

然後在RatingControl添加delegate VAR:

var delegate: RatingControlDelegate? 

然後在ratingButtonTapped(:_)末增加委託方法:

func ratingButtonTapped(button: UIButton) { 
    // ... end of func 
    delegate?.ratingButtonTapped?(self) 
} 

DetailsViewControllerviewDidLoad(:_)

ratingControl1.delegate = self 
ratingControl2.delegate = self 
ratingControl3.delegate = self 
ratingControl4.delegate = self 

而且在RatingControlDelegateDetailsViewController實施你必須ratingControl.rating變量:

func ratingButtonTapped(_ ratingControl: RatingControl) { 
    ratingControl.rating // shows your rating 

    // detect which instance has been clicked 
    switch ratingControl { 
    case ratingControl1: 

    case ratingControl2: 

    // etc. 
    } 
} 
1

我已經使用HCSStarRatingView在我的項目 中顯示星號取數,您可以在下面查看相同的鏈接。

它通過 (例viwFeedBackStar.value)給我提供明星價值

https://github.com/hsousa/HCSStarRatingView

我知道這是不同的庫,按您的實現,但它確實有效

獲取星級ratting在ios

1

無需對代碼進行任何更改。只需訪問使用.rating屬性的當前評級值。

所以視圖控制器類中,當你需要訪問的評價值只寫

print(ratingControl1.rating) 
print(ratingControl2.rating) 

,但如果你需要訪問的評價值作爲用戶水龍頭上的任何等級的控制,然後用Aleksandr Zarubin答案modyfy你的代碼。

+0

謝謝。這是直截了當的,我正在尋找的方法。 – dacscan3669