上一个 segue 在不同的按钮上按时重复 [英] Previous segue repeats on time on different button

查看:32
本文介绍了上一个 segue 在不同的按钮上按时重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽力向您解释我的问题.我正在尝试使用圆形动画构建一种时间表.

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

我有 2 个视图:第一个包含 6 个按钮,标题从 MONDAY 到 SATURDAY.当用户点击一个按钮时,它会模态地呈现第二个视图,它只包含一个没有标题的按钮;第二个视图将采用前一个 VC:ScheduleDaysViewController 中的背景颜色和按钮的标题.在第二个视图中,如果用户单击按钮,它将关闭视图并传递给 ScheduleDaysVC.

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.

正如您从代码中看到的,这是通过在 ScheduleDaysVC 中设置 2 个变量来完成的:dayToPass 和 colorToPass.这些变量根据触发的 IBAction 和转场之前发生变化,通过准备转场"功能,我设置了 ScheduleViewController 的变量 titoloPulsante 和 backgroundColor.

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.

这里的问题是,当我尝试使用该应用程序时,我单击了 MONDAY,它会打开第二个视图,其中包含 MONDAY 按钮的标题和 MONDAY 的颜色(渐变为红色);在此之后,我关闭该视图,然后单击 TUESDAY,它再次打开带有 MONDAY 标题和红色的第二个视图.如果我返回到天视图并在星期二再次单击它最终将打开标题为星期二和正确颜色的第二个视图,因此,紫色.所有按钮都会发生这种情况,FRIDAY 除外.因此,如果我从 TUESDAY 视图返回后单击 WEDNESDAY,它将再次打开 TUESDAY 视图,如果我返回并重新单击 WEDNESDAY,它将打开正确的视图,但是如果我在我之后单击 FRIDAY 或 MONDAY从 WEDNESDAY 视图返回,它将立即打开正确的视图而不重复前一个视图.

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.

我无法理解代码有什么问题,因为每个 IBAction 的代码基本相同,并且它在 MONDAY 和 FRIDAY 起作用,但是如果我点击其他日子,我将打开以前的视图.我还发布了一种结果应用程序的架构.感谢您的耐心等待,如有任何错误或错误的解释,请见谅.

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.

问题架构

STORYBOARD SEGUES:每个按钮都一样

import UIKit

class ScheduleDaysViewController: UIViewController,UIViewControllerTransitioningDelegate {

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 {

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

}

}

推荐答案

根据链接(如何在iOS中prepareForSegue之前执行按钮按下动作?),只有mondayPressed"会在prepare"之前执行.所有其他函数(tuesdayPressed,...) 将在准备"之后执行.

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".

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

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

这篇关于上一个 segue 在不同的按钮上按时重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆