Search code examples
iosswiftuiviewuislider

UISliders and UILabels and viewControllers


How would I be able to increase/decrease the size of my UILabel by using UISlider which is in a different viewController?

I have a viewController1 that has the UILabel1 and I have viewController2 which has a UISlider. With the UISlider I have another label,UILabel2, just to see how big the text will be. I want UILabel1 to increase/decrease also instead of just one label to increase/decrease.

The code being used for UISLider is,

  @IBOutlet weak var label: UILabel!
  @IBOutlet weak var slider: UISlider!

  @IBAction func sizeChanged(sender: UISlider) {
    let senderValue = CGFloat(sender.value)
    label.font = UIFont(name: label.font.fontName, size: senderValue)
}

This code with UILabel is for viewController2 and I want to change the size of another UILabel thats in viewController1.

This is viewController1:

import UIKit

class ViewController1: ViewController {


@IBOutlet weak var label1: UILabel!

@IBOutlet weak var scrollView1: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()

    scrollView1.contentSize.height = 5000

    scrollView1.contentSize.width = 375


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

This is viewController2:

 @IBOutlet weak var label: UILabel!
@IBOutlet weak var slider: UISlider!

@IBAction func sizeChanged(sender: UISlider) {
    let senderValue = CGFloat(sender.value)
    label.font = UIFont(name: label.font.fontName, size: senderValue)
}




override func viewDidLoad() {
    super.viewDidLoad()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Any help would be great.


Solution

  • In computer programming, when something seems difficult or complicated, you should look for ways to break it down into smaller problems that are easier to solve. Yours is a fine example. You asked:

    How would I be able to increase/decrease the size of my UILabel by using UISlider which is in a different viewController?

    So, what are the steps that anyone would need to perform to make this happen? There are basically three steps here:

    1. Get a slider's value.
    2. Send a value from one view controller to another.
    3. Use a value to set the size of a label.

    How do I get a slider's value? This is straightforward. Set the slider's target to the view controller that manages it and its action to some action in the view controller. That method will be called when the slider changes. The action will look like this:

    @IBAction func sliderValueChanged(sender: UISlider) {
        let value = sender.value
        // do something with the value
    }
    

    How do I send data from one view controller to another? There are lots of questions on SO that cover this. Of those, Passing Data Between View Controllers is perhaps the canonical one. There are lots of options, including:

    • Have one view controller call a method in the other. If the view controller with the slider has a reference to the one with the label, it only has to call some method when the slider changes to pass the new value to the other controller. Or, maybe it's the view controller with the label that has a reference to the one with the slider, which is pretty typical if the slider's controller is created by the label's controller. In that case, the label's controller can call a method to retrieve the value.

    • Broadcast the data to anyone who is listening using notifications. When the slider changes, it's action can post a notification with the new value. Any object, including the controller with the label, can listen for that notification and act on it.

    • Use a proper data model. The MVC (model-view-controller) paradigm is big in Cocoa, and if your app is anything beyond trivial it should have its own data model. That model may be a reasonable place to store the slider's latest setting, and the controller with the label can read it from there when its view appears.

    • Stash the value somewhere. Global variables are a short path to a badly design application, but their simplicity is appealing to beginners. A better choice may be the defaults system, which at least lets the value persist when the app quits.

    So, lots of options there. Forget about the slider and the label and think about how the view controllers in your app should communicate with each other. Once you figure that out, the slider setting is just one more thing that they have to say to each other. The style you choose will tell you what to put in the action method above in place of the comment.

    How do I set the size of a label? It's a little unclear what you mean by setting the size. Do you want to change the font size, or the width of the label, or the width and height? In any case, there are accessors for all the properties of a label that you might want to set, so check out the docs. When the label's view controller gets a new value via one of the methods above, it should update the appropriate property of the label. You typically connect the label to an IBOutlet property in the view controller to give the controller easy access to the label.