0

I'll try to explain you my problem at my best.I'm trying to build a kind of schedule using a circular animation.

I have 2 views: the first one contains 6 buttons with the titles from MONDAY to SATURDAY. When the user clicks on a button it will present modally the second view, which contains only one button with no title; this second view will take the backgroundColor and the title of the button from the previous VC: ScheduleDaysViewController. In the second view ,if the user clicks on the button it will close the view and pass to the ScheduleDaysVC.

As you can see from the code this is done by setting 2 variables in the ScheduleDaysVC: dayToPass and colorToPass. These variables change according to the IBAction triggered and before the segue, with the function "prepare for segue" i set the variables titoloPulsante and backgroundColor of the ScheduleViewController.

The problem here is that when i try the app, i click on MONDAY and it opens the second view with the title of the button MONDAY and the color of monday which is a gradient of RED; after this i dismiss the view and i click on TUESDAY and it opens again the second view with MONDAY title and red color. If i return back to the days view and re-click a second time on tuesday it will finally open the second view with title TUESDAY and right color,hence, violet. It happens with all the buttons, except from FRIDAY. So, if i click on WEDNESDAY after i returned from the TUESDAY view it will open another time the TUESDAY view and if i return back and re-click on WEDNESDAY it will open the right view, but if i click on FRIDAY or MONDAY after i returned from the WEDNESDAY view it will immediately open the right view without repeating the previous one.

I'm not able to understand what's wrong with the code becuase it's basically the same for every IBAction and it functions with MONDAY and FRIDAY but if i click on other days i will open the previous view.I also post a kind of schema of the result application. Thank you for the patience and excuse me for any errors or bad explanations.

ISSUE SCHEMA

STORYBOARD SEGUES:SAME FOR EVERY BUTTON

import UIKit

class ScheduleDaysViewController: UIViewController,UIViewControllerTransitioningDelegate {

let transition = CircularAnimation()
var dayToPass: String = ""
var colorToPass = UIColor()
@IBOutlet var mondayButton: UIButton!
@IBOutlet var tuesdayButton: UIButton!
@IBOutlet var wednesdayButton: UIButton!
@IBOutlet var thursdayButton: UIButton!
@IBOutlet var fridayButton: UIButton!
@IBOutlet var saturdayButton: UIButton!



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let schedule = segue.destination as! ScheduleViewController
    schedule.transitioningDelegate = self
    schedule.modalPresentationStyle = .custom
    schedule.titoloPulsante = dayToPass
    schedule.backgroundColor = colorToPass

    print("\n\n\nTITLE : \(dayToPass)")
    print("\n\n\nDAY : \(colorToPass)")

}

/////////
@IBAction func mondayPressed(_ sender: Any) {

    print("\n\n\nMONDAY")
    transition.startingPoint = mondayButton.center
    transition.circleColor = mondayButton.backgroundColor!
    dayToPass = (mondayButton.titleLabel?.text)!
    colorToPass = mondayButton.backgroundColor!
}
/////////
@IBAction func tuesdayPressed(_ sender: Any) {
    print("\n\n\nTUESDAY")
    transition.startingPoint = tuesdayButton.center
    transition.circleColor = tuesdayButton.backgroundColor!
    dayToPass = (tuesdayButton.titleLabel?.text)!
    colorToPass = tuesdayButton.backgroundColor!

}
/////////
@IBAction func thursdayPressed(_ sender: Any) {
    print("\n\n\nTHURSDAY")
    transition.startingPoint = thursdayButton.center
    transition.circleColor = thursdayButton.backgroundColor!
    dayToPass = (thursdayButton.titleLabel?.text)!
    colorToPass = thursdayButton.backgroundColor!


}
/////////
@IBAction func fridayPressed(_ sender: Any) {
    transition.startingPoint = fridayButton.center
    transition.circleColor = fridayButton.backgroundColor!
    dayToPass = (fridayButton.titleLabel?.text)!
    colorToPass = fridayButton.backgroundColor!



}
/////////
@IBAction func wednesdayPressed(_ sender: Any) {
    transition.startingPoint = wednesdayButton.center
    transition.circleColor = wednesdayButton.backgroundColor!
    dayToPass = (wednesdayButton.titleLabel?.text)!
    colorToPass = wednesdayButton.backgroundColor!

}
/////////
@IBAction func saturdayPressed(_ sender: Any) {
    transition.startingPoint = saturdayButton.center
    transition.circleColor = saturdayButton.backgroundColor!
    dayToPass = (saturdayButton.titleLabel?.text)!
    colorToPass = saturdayButton.backgroundColor!


}
/////////
override func viewDidLoad() {
    super.viewDidLoad()

     mondayButton.layer.cornerRadius = mondayButton.frame.size.width / 2
    tuesdayButton.layer.cornerRadius = tuesdayButton.frame.size.width / 2
    wednesdayButton.layer.cornerRadius = wednesdayButton.frame.size.width / 2
    thursdayButton.layer.cornerRadius = thursdayButton.frame.size.width / 2
    fridayButton.layer.cornerRadius = fridayButton.frame.size.width / 2
    saturdayButton.layer.cornerRadius = saturdayButton.frame.size.width / 2


    mondayButton.backgroundColor = hexStringToUIColor(hex: "#FFCDD2")
    tuesdayButton.backgroundColor = hexStringToUIColor(hex: "#E1BEE7")
    wednesdayButton.backgroundColor = hexStringToUIColor(hex: "#C5CAE9")
    thursdayButton.backgroundColor = hexStringToUIColor(hex: "#B2DFDB")
    fridayButton.backgroundColor = hexStringToUIColor(hex: "#C8E6C9")
    saturdayButton.backgroundColor = hexStringToUIColor(hex: "#FFECB3")


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    transition.transitionMode = .dismiss

    return transition
}

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

    transition.transitionMode = .present

    return transition

}

func hexStringToUIColor (hex:String) -> UIColor {
    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

    if (cString.hasPrefix("#")) {
        cString.remove(at: cString.startIndex)
    }

    if ((cString.characters.count) != 6) {
        return UIColor.gray
    }

    var rgbValue:UInt32 = 0
    Scanner(string: cString).scanHexInt32(&rgbValue)

    return UIColor(
        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}

}

import UIKit

class ScheduleViewController: UIViewController {

var titoloPulsante: String?
var backgroundColor = UIColor()

@IBOutlet var dismissButton: UIButton!

@IBAction func dayButtonPressed(_ sender: Any) {

    titoloPulsante = nil

    self.dismiss(animated: true, completion: nil)

}
override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = backgroundColor
    dismissButton.backgroundColor = backgroundColor
    navigationController?.navigationBar.isHidden = true

    dismissButton.setTitle(titoloPulsante, for:.normal)
    dismissButton.layer.cornerRadius = dismissButton.frame.size.width / 2

        }



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

1 Answers1

0

As per the link(How to execute button press action before prepareForSegue in iOS?), only "mondayPressed" will execute before "prepare". All the other functions(tuesdayPressed,...) will execute after "prepare".

After changing the function name from "tuesdayPressed" to "btntuesdayPressed", its working fine.

Vini App
  • 7,339
  • 2
  • 26
  • 43