xcode 在 Swift 中访问标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30856267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:18:35  来源:igfitidea点击:

Access label text in Swift

iosxcodeswift

提问by puks1978

I have created a timer class in swift and when a user clicks a button I segue to another view and pass data between the two views. The time class uses 3 separate labels for hour, minute and second however I would like to pass all 3 in a single variable.

我在 swift 中创建了一个计时器类,当用户单击一个按钮时,我会转到另一个视图并在两个视图之间传递数据。时间类使用 3 个单独的标签来表示小时、分钟和秒,但是我想在一个变量中传递所有 3 个标签。

My question is, how do I access the text inside a label. If I use "\(hourLabel.text)"(for example) I get a message "Optional(00)".

我的问题是,如何访问标签内的文本。如果我使用"\(hourLabel.text)"(例如)我会收到一条消息"Optional(00)"

回答by Duncan C

If you're trying to access another view controller's view objects (a UILabel, for example) don't do that. It violates the principle of encapsulation, and also often doesn't work.

如果您尝试访问另一个视图控制器的视图对象(例如 UILabel),请不要这样做。它违反了封装的原则,也经常行不通。

If try to evaluate hourLabel.text where hourLabel is an outlet in your current view controller, the outlet link is probably broken (and nil.)

如果尝试评估hourLabel.text,其中hourLabel 是当前视图控制器中的出口,则出口链接可能已损坏(并且为零。)

Post the actual code you are trying to use.

发布您尝试使用的实际代码。

回答by Binladenit Tran

Why don't you try this...

你为什么不试试这个...

if(!hourText.text){
// Do something...
}

回答by nachshon f

Use this...

用这个...

if hourLabel.text != "" {
println("\(hourLabel.text!)")
}