将UISlider的值传递到上一页。

huangapple go评论57阅读模式
英文:

Passing the UISlider value to the previous page

问题

我正在制作一个番茄钟应用,并使用 UISlider 调整专注时间。我可以在定义 UISlider 的屏幕上访问 UISlider 的值。但是我想切换到先前的屏幕,但我失败了。我的第一页是 ViewController(),第二页是 SettingsVC()。

这是我在软件行业的第5个月。
如果您能帮忙,将会非常开心。

第一页

import UIKit

class ViewController: UIViewController {
  
    let actionButton = SFActionButton()
    var minutesLabel = SFTimeLabel(text: "25")
    let secondsLabel = SFTimeLabel(text: "00")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
    }
    
    @objc func controll(){
        print("Slider Value : \(SettingsVC().focusTimeSlider.value)")
        print("Working")
    }
    
    
    @objc func settingsAction(){
        let rootVC = SettingsVC()
        let navVC = UINavigationController(rootViewController: rootVC)
        navVC.modalPresentationStyle = .fullScreen
        self.present(navVC, animated: true, completion: nil)
    }
    
}

这段代码是我的第一页。

第二页

import UIKit

class SettingsVC: UIViewController {
 
    //MARK: - 滑块
    
    var focusTimeSlider = UISlider()
    let shortBreakTimeSlider = UISlider()
    let longBreakTimeSlider = UISlider()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        title = "Settings"
    }
    
    //MARK: - 完成按钮
    func addCloseButton(){
        let button = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClose))
        navigationItem.rightBarButtonItem = button
    }
    
    @objc func onClose(){
        self.dismiss(animated: true, completion: nil)

        print("Focus Time       : \(Int(focusTimeSlider.value))")
        print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
        print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
    }
    
    
    //MARK: - 滑块动作
    
    func layoutFocusTimeSlider(){
        focusTimeSlider.addTarget(self, action: #selector(focusSliderValueChanged(sender:)), for: .valueChanged)
    }
    
    @objc func focusSliderValueChanged(sender: UISlider){
        focusTimeSliderValueLabel.text = String(Int(focusTimeSlider.value))
    }
    
    func layoutShortBreakTimeSlider(){
        shortBreakTimeSlider.addTarget(self, action: #selector(shortSliderValueChanged), for: .valueChanged)
    }
    
    @objc func shortSliderValueChanged(){
        shortBreakTimeSliderValueLabel.text = String(Int(shortBreakTimeSlider.value))
    }

    func layoutLongBreakTimeSlider(){
        longBreakTimeSlider.addTarget(self, action: #selector(longSliderValueChanged), for: .valueChanged)
    }

    @objc func longSliderValueChanged(){
        longBreakTimeSliderValueLabel.text = String(Int(longBreakTimeSlider.value))
    }
}

这段代码是我的第二页。

英文:

I'm making a Pomodoro app and adjusting the focus time using UISlider.I can access the UISlider value on the screen where I define the UISlider. But I want to switch to the previous screen. I failed. My first page is ViewController(), my second page is SettingsVC().

It's my 5th month in software.
Very happy if you help.

Firs Page

import UIKit

class ViewController: UIViewController {
  
    let actionButton = SFActionButton()
    var minutesLabel = SFTimeLabel(text: "25")
    let secondsLabel = SFTimeLabel(text: "00")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
    }
    
    @objc func controll(){
        print("Slider Value : \(SettingsVC().focusTimeSlider.value)")
        print("Working")
    }
    
    
    @objc func settingsAction(){
        let rootVC = SettingsVC()
        let navVC = UINavigationController(rootViewController: rootVC)
        navVC.modalPresentationStyle = .fullScreen
        self.present(navVC, animated: true, completion: nil)
    }
    
}

This code page is my first page.

Second Page

import UIKit

class SettingsVC: UIViewController {
 
    
    //MARK: - Sliders
    
    var focusTimeSlider = UISlider()
    let shortBreakTimeSlider = UISlider()
    let longBreakTimeSlider = UISlider()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        title = "Settings"
    }
    
    //MARK: - Done Button
    func addCloseButton(){
        let button = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onClose))
        navigationItem.rightBarButtonItem = button
    }
    
    @objc func onClose(){
        self.dismiss(animated: true, completion: nil)

        
        print("Focus Time       : \(Int(focusTimeSlider.value))")
        print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
        print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
    }
    
    
    //MARK: - Slider Actions
    
    func layoutFocusTimeSlider(){
        focusTimeSlider.addTarget(self, action: #selector(focusSliderValueChanged(sender:)), for: .valueChanged)
        
    }
    
    @objc func focusSliderValueChanged(sender: UISlider){
        focusTimeSliderValueLabel.text = String(Int(focusTimeSlider.value))
    }
    
    func layoutShortBreakTimeSlider(){
        shortBreakTimeSlider.addTarget(self, action: #selector(shortSliderValueChanged), for: .valueChanged)
    }
    
    @objc func shortSliderValueChanged(){
        shortBreakTimeSliderValueLabel.text = String(Int(shortBreakTimeSlider.value))
    }

    func layoutLongBreakTimeSlider(){
        longBreakTimeSlider.addTarget(self, action: #selector(longSliderValueChanged), for: .valueChanged)
    }

    @objc func longSliderValueChanged(){
        longBreakTimeSliderValueLabel.text = String(Int(longBreakTimeSlider.value))
    }
}

This code page is my second page.

答案1

得分: 0

第一个解决方案
第一个解决方案是在第一个 ViewController 中添加一个新的函数:

func sliderValue(value: CGFloat) {
    // 在这里添加您想要执行的操作,使用这个数值
}

接着在设置视图中添加一个变量,用于保存父视图实例:

weak var parentView: UIViewController?

当你呈现设置视图控制器时,在呈现之前将父视图控制器赋给 parentView 实例:

@objc func settingsAction(){
    let rootVC = SettingsVC()
    rootVC.parentView = self
    let navVC = UINavigationController(rootViewController: rootVC)
    navVC.modalPresentationStyle = .fullScreen
    self.present(navVC, animated: true, completion: nil)
}

在关闭设置视图之前调用 sliderValue 函数:

@objc func onClose(){
    if let parentView = parentView as? ViewController {
        parentView.sliderValue(value: focusTimeSlider.value)
    }
    self.dismiss(animated: true, completion: nil)
    
    print("Focus Time       : \(Int(focusTimeSlider.value))")
    print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
    print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
}

第二个解决方案
第二个解决方案同样是添加 sliderValue(value:) 函数,但是不创建 parentView 变量,而是使用以下方法:

@objc func onClose(){
    if let viewController = presentingViewController as? ViewController {
        viewController.sliderValue(value: focusTimeSlider.value)
    }
    self.dismiss(animated: true, completion: nil)
    
    print("Focus Time       : \(Int(focusTimeSlider.value))")
    print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
    print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
}

presentingViewController 这个变量保存着呈现的视图控制器,在这种情况下会保存 ViewController 的值,然后您需要将其转换类型以访问 sliderValue 函数。您可以在苹果文档的 presentingViewController 中找到更多关于这个属性的信息。

英文:

I have two solutions
First solution:
You can add a new function in the first ViewController

func sliderValue(value: CGFloat) {
// add what you want to do here with the value
}

then you add variable in the settings view will hold the parentView instance
like this

weak var parentView: UIViewController?

and when you presenting the settings view controller you assign the parent view controller to the parentView instance before presenting

@objc func settingsAction(){
    let rootVC = SettingsVC()
    rootVC.parentView = self
    let navVC = UINavigationController(rootViewController: rootVC)
    navVC.modalPresentationStyle = .fullScreen
    self.present(navVC, animated: true, completion: nil)
}

then before dismissing the settings view you call the function sliderValue

@objc func onClose(){
    if let parentView = parentView as? ViewController {
        parentView.sliderValue(value: focusTimeSlider.value)
    }
    self.dismiss(animated: true, completion: nil)

    
    print("Focus Time       : \(Int(focusTimeSlider.value))")
    print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
    print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
}

Second Solution:
You will add the sliderValue(value:) function but you will not create the parentView variable instead you will do as follow

@objc func onClose(){
    if let viewController = presentingViewController as? ViewController {
        viewController.sliderValue(value: focusTimeSlider.value)
    }
    self.dismiss(animated: true, completion: nil)

    
    print("Focus Time       : \(Int(focusTimeSlider.value))")
    print("Short Break Time : \(Int(shortBreakTimeSlider.value))")
    print("Long Break Time  : \(Int(longBreakTimeSlider.value))")
}

self.presentingViewController this variable holds the presenting view controller which in this case will hold the value for the ViewController then you will need to downcast it to be able to access the sliderValue function . you can find more about this property in the apple documentation presentingViewController

huangapple
  • 本文由 发表于 2023年2月19日 16:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定