如何在swift按钮中动态更改文本?

时间:2015-07-20 08:07:47

标签: ios swift

当用户点击"下载"按钮我需要更改按钮的标题 "装载..."但是点数必须从1动态变化到3个。如何在一定时间内(5-10秒)制作它?或者我只需要多次setTitle?

button.setTitle("loading.", forState: .Normal)

4 个答案:

答案 0 :(得分:1)

您需要使用

button.setTitle("loading.", forState: .Normal)
button.setTitle("loading..", forState: .Normal)
button.setTitle("loading...", forState: .Normal)

每当您想要更改点数时 延迟后或需要更新按钮时,请拨打以上其中一项。

编辑:为了防止按钮的标题闪烁(如teamnorge所指出),请使用:

UIView.performWithoutAnimation {
    button.setTitle("loading...", forState: .Normal)
}

答案 1 :(得分:0)

button.setTitle("my text here", forState: .Normal)

答案 2 :(得分:0)

@IBAction func renameClassButton(sender: AnyObject) {
     classTopButton.text = "\(classTopTextField)"
}

答案 3 :(得分:0)

你可以这样:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var loading: UIButton!

    //Create an array for your button name
    let buttonNameArray = ["loading.","loading..","loading..."]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func loadingButton(sender: AnyObject) {
        //Set delay time for changing name
        var delayTime = 1.0

        for name in buttonNameArray {

            self.delay(delayTime++){
                self.loading.setTitle(name, forState: .Normal)
            }
        } 
    }

    //Function for delay
    func delay(delay:Double, closure:()->()) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(delay * Double(NSEC_PER_SEC))),dispatch_get_main_queue(), closure)
    }
}

你的结果将是:

enter image description here

希望这会有所帮助。