In this article, we will implement the review function only using stars and comments.
If you look at this article, you can implement only the review function using stars and comments as shown below.
--Programming beginners who want to develop iPhone apps using Swift --Those who want to implement the review function
--Implementation method
--Library installation (pod install)
--Create Main.storyboard
--Create RatingViewController.xib
--Coding
--Association
--Reference site
Now, let's actually implement it.
Fill in the podfile as shown below and install the two libraries.
pod 'PopupDialog', '~> 1.1'
pod 'Cosmos', '~> 23.0'
Main.storyboard
Place the required parts in Main.storyboard
as shown in the image above.
--Label to display the number of stars --Label for displaying comments --Button to perform a review
RatingViewController.xib
RatingViewController.xib
Create the UI as shown in the image below.
RatingViewController.xib
fileRatingViewController.xib
to FreeformRatingViewController.xib
--Two Label --TextField for commenting --UIView to select a star (CosmosView is set to Class as shown in the image below)
ViewController.swift
ViewController.swift
import UIKit
import PopupDialog
class ViewController: UIViewController {
@IBOutlet weak var starLabel: UILabel!
@IBOutlet weak var commentLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func showCustomDialog(animated: Bool = true) {
// Create a custom view controller
let ratingVC = RatingViewController(nibName: "RatingViewController", bundle: nil)
// Create the dialog
let popup = PopupDialog(viewController: ratingVC,
buttonAlignment: .horizontal,
transitionStyle: .bounceDown,
tapGestureDismissal: true,
panGestureDismissal: false)
// Create first button
let buttonOne = CancelButton(title: "CANCEL", height: 60) {
self.starLabel.text = "You canceled the rating dialog"
}
// Create second button
let buttonTwo = DefaultButton(title: "RATE", height: 60) {
self.starLabel.text = "You rated \(ratingVC.cosmosStarRating.rating) stars"
self.commentLabel.text = ratingVC.commentTextField.text
}
// Add buttons to dialog
popup.addButtons([buttonOne, buttonTwo])
// Present dialog
present(popup, animated: animated, completion: nil)
}
@IBAction func showCustomDialogTapped(_ sender: UIButton) {
showCustomDialog()
}
}
RatingViewController.swift
RatingViewController.swift
import UIKit
import Cosmos
class RatingViewController: UIViewController {
@IBOutlet weak var cosmosStarRating: CosmosView!
@IBOutlet weak var commentTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Default 3 for the number of stars.Set to 0
cosmosStarRating.rating = 3.0
commentTextField.delegate = self
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(endEditing)))
}
@objc func endEditing() {
view.endEditing(true)
}
}
//Processing related to textField
extension RatingViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
endEditing()
return true
}
}
Finally, don't forget to associate. This is completed!
-PopupDialog's GitHub -Cosmos's GitHub
Recommended Posts