上一个 segue 在不同的按钮上按时重复

Previous segue repeats on time on different button

我会尝试向您解释我在 best.I 尝试使用圆形动画构建一种时间表时遇到的问题。

我有 2 个视图:第一个包含 6 个按钮,标题从星期一到星期六。当用户点击一个按钮时,它将以模态方式呈现第二个视图,其中仅包含一个没有标题的按钮;第二个视图将采用前一个 VC: ScheduleDaysViewController 中的背景颜色和按钮标题。 在第二个视图中,如果用户单击按钮,它将关闭视图并传递给 ScheduleDaysVC。

正如您从代码中看到的那样,这是通过在 ScheduleDaysVC 中设置 2 个变量来完成的:dayToPass 和 colorToPass。这些变量根据触发的 IBAction 和 segue 之前的变化而变化,使用函数 "prepare for segue" 我设置了 ScheduleViewController 的变量 titoloPulsante 和 backgroundColor。

这里的问题是,当我尝试该应用程序时,我单击“星期一”,它会打开第二个视图,其中包含按钮“星期一”的标题和星期一的颜色,即红色渐变;在此之后我关闭视图并单击星期二,它再次打开带有星期一标题和红色的第二个视图。 如果我 return 回到天视图并在星期二再次 re-click 它将最终打开第二个视图,标题为星期二和正确的颜色,因此是紫罗兰色。它发生在所有按钮上,除了 FRIDAY。因此,如果我在 return 从星期二视图编辑后单击星期三,它将再次打开星期二视图,如果我 return 返回并在星期三 re-click 它将打开正确的视图,但是如果我在 return 从 WEDNESDAY 视图编辑后单击 FRIDAY 或 MONDAY,它将立即打开正确的视图,而不会重复上一个视图。

我无法理解代码有什么问题,因为每个 IBAction 基本上都是一样的,它适用于星期一和星期五,但如果我点击其他日子,我也会打开前一个 view.I post 结果应用的一种模式。感谢您的耐心等待,如有错误或解释不当,请多多包涵。

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()

}

}

根据 link(How to execute button press action before prepareForSegue in iOS?),只有 "mondayPressed" 会在 "prepare" 之前执行。所有其他功能(tuesdayPressed,...)将在 "prepare".

之后执行

将函数名从 "tuesdayPressed" 更改为 "btntuesdayPressed" 后,它工作正常。