单击按钮后,将标签中的文本更改为“hi”

时间:2014-10-15 17:23:51

标签: ios swift uiviewcontroller

这有什么问题,它是指在您点击按钮后将标签中的文字更改为“hi”。

ViewController.swift:

//
//  ViewController.swift
//  HelloSwift
//
//  Created by Abdel Rahman Osman on 10/13/14.
//  Copyright (c) 2014 Abdel Rahman Osman. All rights reserved.
//

import UIKit


class ViewController: UIViewController {

    @IBOutlet weak var TextGoesHere: UILabel!
    @IBAction func PrintText(sender: AnyObject) {
        var Text = TextGoesHere.text
        Text = "Hi"
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
            }

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


}

请帮我修复此代码,因为我不熟悉应用开发。

1 个答案:

答案 0 :(得分:1)

您正在做的是在标签中创建文本的副本,丢弃它并重新分配新值,然后在变量超出范围时再次丢弃它。

正确的方法是将文本直接分配给text属性:

@IBAction func PrintText(sender: AnyObject) {
    TextGoesHere.text = "Hi"
}