当前位置:  开发笔记 > IOS > 正文

以编程方式在Swift中使用UISlider更改UILabel

如何解决《以编程方式在Swift中使用UISlider更改UILabel》经验,为你挑选了1个好方法。

我正在学习Swift并尝试使用Slider以编程方式更改Label值.我在func中收到错误,说paybackLabel是一个未解析的标识符.该怎么做?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Create Label
        let paybackLabel = UILabel(frame: CGRectMake(0, 0, 300, 41))
        paybackLabel.center = CGPointMake(190, 284)
        paybackLabel.textAlignment = NSTextAlignment.Center
        paybackLabel.font = UIFont(name: paybackLabel.font.fontName, size: 40)
        paybackLabel.textColor = UIColor.whiteColor()
        paybackLabel.text = "Hello World"
        self.view.addSubview(paybackLabel)

        //Create Slider
        let paybackSlider = UISlider(frame: CGRectMake (45,546,310,31))
        paybackSlider.minimumValue = 0
        paybackSlider.maximumValue = 1000
        paybackSlider.continuous = true
        paybackSlider.tintColor = UIColor.blueColor()
        paybackSlider.value = 500
        paybackSlider.addTarget(self, action: "paybackSliderValueDidChange:",forControlEvents: .ValueChanged)

        self.view.addSubview(paybackSlider)

    }

    func paybackSliderValueDidChange(sender: UISlider!)
    {
        print("payback value: \(sender.value)")
        paybackLabel.text = "\(sender.value)"

    }


}

Pavan Gandhi.. 6

正如我在这里建议的那样纠正你的代码.

1.)为Outside定义两个变量以公开访问和修改标签值

var paybackLabel : UILabel = UILabel()
var paybackSlider : UISlider = UISlider()

2.)将以下代码放在viewDidload()中

//Create Label

paybackLabel = UILabel(frame: CGRectMake(10, 20, 100, 41))
paybackLabel.center = CGPointMake(190, 61)
paybackLabel.textAlignment = NSTextAlignment.Center
paybackLabel.font = UIFont(name: paybackLabel.font.fontName, size:10)
paybackLabel.textColor = UIColor.blueColor()
paybackLabel.text = "Hello World"
self.view.addSubview(paybackLabel)

//Create Slider

paybackSlider = UISlider(frame: CGRectMake (45,70,310,31))
paybackSlider.minimumValue = 0
paybackSlider.maximumValue = 1000
paybackSlider.continuous = true
paybackSlider.tintColor = UIColor.blueColor()
paybackSlider.value = 500
paybackSlider.addTarget(self, action: "paybackSliderValueDidChange:",forControlEvents: .ValueChanged)
paybackLabel.text = "\(paybackSlider.value)"
self.view.addSubview(paybackSlider)

3.)不要通过更改slidervaluechanged事件来放置在标签上显示值的更改功能

func paybackSliderValueDidChange(sender: UISlider!)
{
    print("payback value: \(sender.value)")
    paybackLabel.text = "\(sender.value)"
}

结果:

在此输入图像描述

它的工作很好享受..



1> Pavan Gandhi..:

正如我在这里建议的那样纠正你的代码.

1.)为Outside定义两个变量以公开访问和修改标签值

var paybackLabel : UILabel = UILabel()
var paybackSlider : UISlider = UISlider()

2.)将以下代码放在viewDidload()中

//Create Label

paybackLabel = UILabel(frame: CGRectMake(10, 20, 100, 41))
paybackLabel.center = CGPointMake(190, 61)
paybackLabel.textAlignment = NSTextAlignment.Center
paybackLabel.font = UIFont(name: paybackLabel.font.fontName, size:10)
paybackLabel.textColor = UIColor.blueColor()
paybackLabel.text = "Hello World"
self.view.addSubview(paybackLabel)

//Create Slider

paybackSlider = UISlider(frame: CGRectMake (45,70,310,31))
paybackSlider.minimumValue = 0
paybackSlider.maximumValue = 1000
paybackSlider.continuous = true
paybackSlider.tintColor = UIColor.blueColor()
paybackSlider.value = 500
paybackSlider.addTarget(self, action: "paybackSliderValueDidChange:",forControlEvents: .ValueChanged)
paybackLabel.text = "\(paybackSlider.value)"
self.view.addSubview(paybackSlider)

3.)不要通过更改slidervaluechanged事件来放置在标签上显示值的更改功能

func paybackSliderValueDidChange(sender: UISlider!)
{
    print("payback value: \(sender.value)")
    paybackLabel.text = "\(sender.value)"
}

结果:

在此输入图像描述

它的工作很好享受..

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有