0

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.

burnsi
  • 6,194
  • 13
  • 17
  • 27
  • Do not use false tags. There's no SwiftUI in your code. And please explain what "I want to switch to the previous screen. I failed." means. What's your _question_? – matt Feb 19 '23 at 09:15
  • I'm trying to pass the UISlider value to the minutesLabel on my first page, but I haven't been able to. How can I do it ? @matt –  Feb 19 '23 at 10:28
  • 1
    Hmm, passing data between view controllers... Let's see... https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – matt Feb 19 '23 at 13:36
  • Does this answer your question? [Passing data between view controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – HangarRash Feb 19 '23 at 22:18
  • No. But i used callBack method. It's working now. Thank you. –  Feb 20 '23 at 13:01

1 Answers1

0

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

Ahmed Mohiy
  • 190
  • 1
  • 7