0

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.

jas bath
  • 63
  • 2
  • 9

2 Answers2

1

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.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I've looked up many questions related to mine but I haven't seen one that has a UISlider and UILabel. They all use a UITextField to show how delegate works. I did 10 different ways of delegate and all failed on me. Its simple what I want, I just want UILabel from VC1 to VC2 and I want to use that UILabel to increase/decrease which I can figure out. Forget about the increase/decrease for the UILabel, makes it confusing for me as well, I can ask about that in a different question. Thanks for the posts, I've learned some what of delegate. – jas bath May 14 '16 at 16:41
  • @jasbath `UISlider` doesn't use a delegate. As described above, it uses the same target/action pattern that every `UIControl` does. `UITextField` is a control, so can also use target/action, but it does have a delegate. – Caleb May 15 '16 at 03:17
  • I see, well I'm starting to see. You said to get the value of UISlider, I pasted the code onto VC2 which has @IBOutlet weak var slider: UISlider! and I got an error Initialization of immutable value 'value' was never used; consider replacing with assignment to '_' or removing it . You said do something with the value, what would I do. The error is saying that the value was never used. – jas bath May 15 '16 at 07:01
  • I just gace the skeleton of an action method. You need to decide how you want to handle the next step and add code to that method in place of the comment. A simple example would be to store it in the defaults system. – Caleb May 15 '16 at 13:45
  • If I can use UITextfield the same as UISlider, how would I put UISlider in this code, 'override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { if (segue.identifier == "segueTest") { var SettingsVC = segue!.destinationViewController as Settings; SettingsVC.toPass = textField.text } }' – jas bath May 16 '16 at 15:03
  • By writing this code in viewController2 I can go into viewController1 and type the code 'var toPass: String!' and then I could do what I wanted to do by changing its size by using the code '@IBAction func sizeChanged(sender: UISlider) { let senderValue = CGFloat(sender.value) label1.font = UIFont(name: label1.font.fontName, size: senderValue) }' – jas bath May 16 '16 at 15:03
  • But even if I get this far how would I use 'toPass' as a UISlider in the code '@IBAction func sizeChanged(sender: UISlider) { let senderValue = CGFloat(sender.value) label1.font = UIFont(name: label1.font.fontName, size: senderValue) }' – jas bath May 16 '16 at 15:08
  • @jasbath Maybe read my answer again: you want to split the task up into the different parts. You don't need to create a font object when the slider changes, you just need to save the value from the slider somewhere so that `viewController2` can fetch it when it's about to display it's view. `viewController2`'s `viewWillAppear` method can then fetch the value and set the label size. – Caleb May 16 '16 at 15:48
  • Will it set the label size for viewController1 and viewController2? – jas bath May 20 '16 at 03:21
0

I think, you can look at NSNotificationCenter's functionality, especially at NSNotification's userInfo: parameter. You can pass your slider's value to userInfo from first VC and then listen to this notification in second VC.

Great example of this method in Objective-C: https://stackoverflow.com/a/7896761

Community
  • 1
  • 1
vanelizarov
  • 1,064
  • 1
  • 8
  • 24