我会尽力向你解释我的问题。我试图用圆形动画制作一种时间表。
我有2个视图:第一个包含6个按钮,标题从星期一到星期六。当用户点击一个按钮时,它将以模态方式呈现第二个视图,该视图只包含一个没有标题的按钮;第二个视图将采用backgroundColor和前一个VC中的按钮标题:ScheduleDaysViewController。 在第二个视图中,如果用户单击该按钮,它将关闭视图并传递给ScheduleDaysVC。
从代码中可以看出,这是通过在ScheduleDaysVC中设置2个变量来完成的:dayToPass和colorToPass。这些变量根据所触发的IBAction和segue之前的变化而变化,其功能是准备segue"我设置了ScheduleViewController的变量titoloPulsante和backgroundColor。
这里的问题是,当我尝试使用该应用程序时,我点击星期一,然后打开第二个视图,其中包含MONDAY按钮的标题和星期一的渐变颜色;在此之后我解散视图并点击星期二,它再次打开第三个星期一标题和红色的视图。 如果我回到天视图并在星期二再次点击它,它将最终打开第二个视图,标题为TUESDAY和正确的颜色,因此,紫色。它发生在所有按钮上,除了星期五。因此,如果我在从星期二视图返回后单击星期三它将再次打开星期二视图,如果我返回并重新点击星期三它将打开右视图,但如果我点击星期五或星期一后我从WEDNESDAY视图返回,它将立即打开右视图而不重复前一个视图。
我无法理解代码的错误,因为它对于每个IBAction基本相同,并且它在星期一和星期五运行但是如果我点击其他日子我会打开上一个视图。我还发布了一种结果应用程序的模式。感谢您的耐心,并原谅我的任何错误或错误的解释。
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()
}
}
答案 0 :(得分:0)
根据链接(How to execute button press action before prepareForSegue in iOS?),只有" mondayPressed"将在"准备"之前执行。所有其他函数(tuesdayPressed,...)将在" prepare"。
之后执行从" tuesdayPressed"更改功能名称后到了" btntuesdayPressed",它的工作正常。