6 Replies
      Latest reply on Aug 16, 2018 8:15 AM by KMT
      Joel14 Level 1 Level 1 (0 points)

        I added the feature so that when a person taps on a button the button highlights this is the code for that so far:

         

          var selectedButton = 0
            
            func hiliteButtons() {
                
                cheap.backgroundColor = .white
                basic.backgroundColor = .white
                luxury.backgroundColor = .white
                platinum.backgroundColor = .white
                switch selectedButtons {
                case cheap.tag :
                    cheap.backgroundColor = UIColor.customBlue
                case basic.tag :
                    basic.backgroundColor = UIColor.customBlue
                case luxury.tag :
                    luxury.backgroundColor = UIColor.customBlue
                case platinum.tag:
                    platinum.backgroundColor = UIColor.customBlue
                default: break
                }
            }
            
            @IBAction func cheap(_ sender: Any) {
                selectedButtons = cheap.tag
                print("selectedButtons", selectedButtons)
                print("cheap.tag", cheap.tag)
                rideCatagory = .cheap
                hiliteButtons()
            }
            
            @IBAction func basic(_ sender: Any) {
                selectedButtons = basic.tag
                print("selectedButtons", selectedButtons)
                print("basic.tag", basic.tag)
                rideCatagory = .basic
                hiliteButtons()
            }
            
            @IBAction func luxury(_ sender: Any) {
                selectedButtons = luxury.tag
                print("selectedButtons", selectedButtons)
                print("luxury.tag", luxury.tag)
                rideCatagory = .luxury
                hiliteButtons()
            }
            
            @IBAction func platinum(_ sender: Any) {
                selectedButtons = platinum.tag
                print("selectedButtons", selectedButtons)
                print("platinum.tag", platinum.tag)
                rideCatagory = .platinum
                hiliteButtons()
            }
            
        

         

        I wanted to know how would I make it so that there is already a pre-selected button for example, for this, I would want basic to be pre-selected when the user is taken to this view.

        • Re: Pre-Selected Highlighted Button
          Claude31 Level 7 Level 7 (4,545 points)

          You should do 2 things:

          - set selectedButtons to the value of cheap.tag

          - call hiliteButtons() in the viewDidLoad

            • Re: Pre-Selected Highlighted Button
              Joel14 Level 1 Level 1 (0 points)

              I set the selected buttons variable and called the hilightButtons() in viewDidLoad, and it highlights the buttons but when I launch the simulator to test it out it highlights the button I want, Then I tap the next button to take me to the next screen and I get a popup from some other code I added. Here is the code:

               

              import UIKit
              import Firebase
              import FirebaseAuth
              import FirebaseDatabase
              
              enum RideType{
                  case cheap
                  case basic
                  case luxury
                  case platinum
                  func description() -> String{
                      switch self{
                      case .cheap: return "Cheap"
                      case .basic: return "Basic"
                      case .luxury: return "Luxury"
                      case .Platinum: return "Platinum"
                      }
                  }
                  
              }
              
              enum rideLength{
                  case sixtyMinuteRide
                  case ninetyMinuteRide
                  case twoHourRide
                  func description() -> String{
                      switch self{
                      case .sixtyMinuteRide : return "60 Minutes"
                      case .ninetyMinuteRide : return "90 Minutes"
                      case .twoHourRide: return "120 Minutes"
                      }
                  }
                  
              }
              
              
              
              class ViewController: UIViewController{
                  
              
                  //MARK: Properties
                  @IBOutlet weak var phoneNumber: UITextField!
                  @IBOutlet weak var verificationCode: UITextField!
                  @IBOutlet weak var firstName: UITextField!
                  @IBOutlet weak var lastName: UITextField!
                  @IBOutlet weak var email: UITextField!
                  @IBOutlet weak var firstName1: UITextField!
                  @IBOutlet weak var lastName1: UITextField!
                  @IBOutlet weak var phoneNumber1: UITextField!
                  @IBOutlet weak var email1: UITextField!
                  @IBOutlet weak var password1: UITextField!
                  var registered = false
                  let registerKey = "RegisterKey"
                  var selectedRideType = 0
                  var selectedRideLength = 0
                  @IBOutlet weak var sixtyMinuteRide: UIButton!
                  @IBOutlet weak var ninetyMinuteRide: UIButton!
                  @IBOutlet weak var twoHourRide: UIButton!
                  @IBOutlet weak var rideDatePicker: UIDatePicker!
                  @IBOutlet weak var segueToPrice: UIButton!
                  @IBOutlet weak var cheap: CustomButton!
                  @IBOutlet weak var basic: CustomButton!
                  @IBOutlet weak var luxury: CustomButton!
                  @IBOutlet weak var platinum: CustomButton!
                  @IBOutlet weak var scheduleRideCancel: UIButton!
                  var rideCatagory: RideType?
                  var rideLength: rideLength?
                  var dateOfRide: Date = Date()
                  
                  
                  
                  
                 
                  
                  override func viewDidLoad() {
                      super.viewDidLoad()
                      let defaults = UserDefaults.standard
                      registered = defaults.bool(forKey: registerKey)
                      // If key does not exist yet, return false
                      if !registered {
                          // performSegue to the view to ask, or open an alert or a popover
                          // If you get the credential
                          defaults.set(true, forKey: registerKey)
                      }  
                      // Do any additional setup after loading the view, typically from a nib.
                       cheap.titleLabel?.adjustsFontSizeToFitWidth = true
                       basic.titleLabel?.adjustsFontSizeToFitWidth = true
                      luxury.titleLabel?.adjustsFontSizeToFitWidth = true
                      platinum.titleLabel?.adjustsFontSizeToFitWidth = true
                      selectedRideLength = 6
                      highlightButtons1()
                      selectedRideType = 2
                      highlightButtons()
                  }
                  
                  //MARK: Save Data
                  //Saves data to Firebase Database
                  var ref: DatabaseReference!
                  
                  @IBAction func saveEmailAccountInfo(_ sender: Any) {
                      ref = Database.database().reference()
                      
                      ref?.child("firstName1").childByAutoId().setValue(firstName1.text)
                      ref?.child("lastName1").childByAutoId().setValue(lastName1.text)
                      ref?.child("phoneNumber1").childByAutoId().setValue(phoneNumber1.text)
                      ref?.child("phoneNumber").childByAutoId().setValue(phoneNumber.text)
                      ref?.child("email1").childByAutoId().setValue(email1.text)
                      ref?.child("password1").childByAutoId().setValue(password1.text)
                  }
                  
                  @IBAction func savephoneNumber(_ sender: Any) {
                      ref = Database.database().reference()
                      
                      ref?.child("phoneNumber").childByAutoId().setValue(phoneNumber.text)
                  }
                  
                  @IBAction func saveEmail(_ sender: Any) {
                      ref = Database.database().reference()
                      
                      if email.text != ""{
                      ref?.child("email").childByAutoId().setValue(email.text)
                      }
                  }
                  
                  @IBAction func saveName(_ sender: Any) {
                      ref = Database.database().reference()
                      
                      if firstName.text != "" && lastName.text != ""{
                      ref?.child("firstName").childByAutoId().setValue(firstName.text)
                      ref?.child("lastName").childByAutoId().setValue(lastName.text)
                      }
                  }
                  
                  //MARK: Phone Verification
                  //Sends code to mobile device
                  @IBAction func sendCode(_ sender: Any) {
                      PhoneAuthProvider.provider().verifyPhoneNumber(phoneNumber.text!, uiDelegate: nil) { (verificationID, error) in
                          if let error = error {
                              print(error as Any)
                              return
                          }
                          // Sign in using the verificationID and the code sent to the user
                          // ...
                          UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
                          
                      }
                  }
                  
                  //Checks verification code
                  @IBAction func enterverificationCode(_ sender: Any) {
                      let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
                      let credential = PhoneAuthProvider.provider().credential(
                          withVerificationID: verificationID!,
                          verificationCode: verificationCode.text!)
                      Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
                          if error != nil {
                              // ...
                              return
                          }
                          // User is signed in
                          // ...
                          if Auth.auth().currentUser != nil {
                              // User is signed in.
                              // ...
                          } else {
                              // No user is signed in.
                              // ...
                          }
                      }
                  }
                  
                  //MARK: Email/Password Verification
                  //Creates account for email/password verification
                  @IBAction func createEmailPasswordAccount(_ sender: Any) {
                      Auth.auth().createUser(withEmail: email1.text!, password: password1.text!) { (authResult, error) in
                          // ...
                      }
                  }
                  
                  @IBAction func signInEmailPasswordAccount(_ sender: Any) {
                      Auth.auth().signIn(withEmail: email1.text!, password: password1.text!) { (user, error) in
                          // ...
                          if Auth.auth().currentUser != nil {
                              // User is signed in.
                              // ...
                          } else {
                              // No user is signed in.
                              // ...
                          }
                      }
                  }
                  
                  @IBAction func signOut(_ sender: Any) {
                      let firebaseAuth = Auth.auth()
                      do {
                          try firebaseAuth.signOut()
                      } catch let signOutError as NSError {
                          print ("Error signing out: %@", signOutError)
                      }
                  }
                  
                  //MARK: Highlight Ride Type
                  
                  func highlightButtons() {
                      
                      cheap.backgroundColor = .white
                      basic.backgroundColor = .white
                      luxury.backgroundColor = .white
                      platinum.backgroundColor = .white
                      switch selectedRideType {
                      case cheap.tag :
                          cheap.backgroundColor = UIColor.customBlue
                      case basic.tag :
                          basic.backgroundColor = UIColor.customBlue
                      case luxury.tag :
                          luxury.backgroundColor = UIColor.customBlue
                      case platinum.tag:
                          platinum.backgroundColor = UIColor.customBlue
                      default: break
                      }
                  }
                  
                  @IBAction func cheap(_ sender: Any) {
                      selectedRideType = cheap.tag
                      print("selectedRideType", selectedRideType)
                      print("cheap.tag", cheap.tag)
                      rideCatagory = .cheap
                      highlightButtons()
                  }
                  
                  @IBAction func basic(_ sender: Any) {
                      selectedRideType = basic.tag
                      print("selectedRideType", selectedRideType)
                      print("basic.tag", basic.tag)
                      rideCatagory = .basic
                      highlightButtons()
                  }
                  
                  @IBAction func luxury(_ sender: Any) {
                      selectedRideType = luxury.tag
                      print("selectedRideType", selectedRideType)
                      print("luxury.tag", luxury.tag)
                      rideCatagory = .luxury
                      highlightButtons()
                  }
                  
                  @IBAction func platinum(_ sender: Any) {
                      selectedRideType = platinum.tag
                      print("selectedRideType", selectedRideType)
                      print("platinum.tag", platinum.tag)
                      rideCatagory = .platinum
                      highlightButtons()
                  }
                  
                  //MARK: Highlight Ride Length
                  
                  func highlightButtons1() {
                      
                      sixtyMinuteRide.backgroundColor = .white
                      ninetyMinuteRide.backgroundColor = .white
                      twoHourRide.backgroundColor = .white
                      switch selectedRideLength {
                      case sixtyMinuteRide.tag :
                          sixtyMinuteRide.backgroundColor = UIColor.customBlue
                      case ninetyMinuteRide.tag :
                          ninetyMinuteRide.backgroundColor = UIColor.customBlue
                      case twoHourRide.tag :
                          twoHourRide.backgroundColor = UIColor.customBlue
                      default: break
                      }
                  }
                  
                  @IBAction func sixtyMinuteRideLength(_ sender: Any) {
                      selectedRideLength = sixtyMinuteRide.tag
                      print("selectedRideLength", selectedRideLength)
                      print("sixtyMinuteRide.tag", sixtyMinuteRide.tag)
                      rideLength = .sixtyMinuteRide
                      highlightButtons1()
                  }
                  
                  @IBAction func ninetyMinuteRideLength(_ sender: Any) {
                      selectedRideLength = ninetyMinuteRide.tag
                      print("selectedRideLength", selectedRideLength)
                      print("ninetyMinuteRide.tag", ninetyMinuteRide.tag)
                      rideLength = .ninetyMinuteRide
                      highlightButtons1()
                  }
                  
                  @IBAction func twoHourRideLength(_ sender: Any) {
                      selectedRideLength = twoHourRide.tag
                      print("selectedRideLength", selectedRideLength)
                      print("twoHourRide.tag", twoHourRide.tag)
                      massageRide = .twoHourRide
                      highlightButtons1()
                  }
                  
              
                  //MARK: Information sent to PriceView
                  @IBAction func setRideDate(_ sender: UIDatePicker) {
                      dateOfRide = sender.date
                  }
                  
                  override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
                      if identifier == "segueToPrice" && rideCatagory == nil {     // When none was selected
                          let alert = UIAlertController(title: "Missing Information", message: "", preferredStyle: .alert)
                          let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                          alert.addAction(okAction)
                          self.present(alert, animated: true, completion: nil)
                          return false
                          // shouldPerform is false, segue will not be executed
                      } else if identifier == "segueToPrice" && rideLength == nil{
                          let alert = UIAlertController(title: "Missing Information", message: "", preferredStyle: .alert)
                          let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                          alert.addAction(okAction)
                          self.present(alert, animated: true, completion: nil)
                          return false
                      }else{
                          return true
                      }
                  }
                  
                  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                      var ridePrice : Float = 0.0
                      guard let rideCatagory = rideCatagory else {
                          print("rideCatagory is nil");
                          return
                      }
                      guard let rideLength = rideLength else {
                          print("rideLength is nil");
                          return
                      }
                      
                      switch rideCatagory {
                      case .cheap :
                          switch rideLength{
                          case .sixtyMinuteRide : ridePrice = 90.00
                          case .ninetyMinuteRide : ridePrice = 120.00
                          case .twoHourRide : ridePrice = 140.00
                          }
                      case .basic :
                          switch rideLength{
                          case .sixtyMinuteRide : ridePrice = 90.00
                          case .ninetyMinuteRide : ridePrice = 120.00
                          case .twoHourRide : ridePrice = 140.00
                          }
                      case .luxury:
                          switch rideLength{
                          case .sixtyMinuteRide : ridePrice = 90.00
                          case .ninetyMinuteRide : ridePrice = 120.00
                          case .twoHourRide : ridePrice = 140.00
                          }
                      case .platinum:
                          switch massageLength{
                          case .sixtyMinuteRide : ridePrice = 90.00
                          case .ninetyMinuteRide : ridePrice = 120.00
                          case .twoHourRide : ridePrice = 140.00
                          }
                      }
                      
                      if segue.identifier == "segueToPrice" {          // The identifier you have given to the segue
                          if let destVC = segue.destination as? PriceView {
                              destVC.price = ridePrice
                              print(destVC.price as Any)
                              destVC.category = rideCatagory
                              print(destVC.category?.description() as Any)
                              destVC.length = rideLength
                              print(destVC.length?.description() as Any)
                              destVC.dateOfRide = dateOfRide
                          }
                      } else {
                          print("Wrong Segue", segue.identifier as Any)
                      }
                  }
                  
                  override func didReceiveMemoryWarning() {
                      super.didReceiveMemoryWarning()
                      // Dispose of any resources that can be recreated.
                  }
              }