I Am trying to build a very simple app:
我正在尝试构建一个非常简单的应用程序:
It will print elements of an array so here are my two questions
它将打印数组的元素,所以这是我的两个问题
in my current code
在我目前的代码中
@IBOutlet weak var moussarLabel: UILabel!
@IBAction func moussarButton(sender: AnyObject) {
var moussar = ["dzv","avazv","sdv dv","dvar3"]
for var i = 0; i<moussar.count; i++ {
moussarLabel.text = "\(moussar[i])"
}
}
I want to know two things:
我想知道两件事:
- when the code executes it automatically print the last element of the array without displaying the other elements
- 当代码执行时,它会自动打印数组的最后一个元素而不显示其他元素
- Say that I want the text in the array to be much much longer, how do i make UILabel to adapt to the text and to allow the user to scroll though the text?
- 假设我希望数组中的文本更长,我如何使UILabel适应文本并允许用户滚动文本?
thanks for the help
谢谢您的帮助
3 个解决方案
#1
0
-
The final element in the array is the last thing the loop assigns to the text label. Sounds like its working as expected. Did you mean to concatenate (add) the strings?
数组中的最后一个元素是循环分配给文本标签的最后一个元素。听起来像预期的那样工作。你的意思是连接(添加)字符串?
-
You can do that with a UITextView https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/
您可以使用UITextView执行此操作https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/
#2
0
First of all, you can't scroll a UILabel so you'll have to use a UITextField
. Read the documentation for details, it shouldn't be too hard when only displaying a single word. For getting the last element of the array, use moussarLabel.last
which returns an optional. As the text property of a text view or a label, respectively, accepts nil
as value, this isn't a problem.
首先,您无法滚动UILabel,因此您必须使用UITextField。阅读文档了解详细信息,只显示一个单词时应该不会太难。要获取数组的最后一个元素,请使用返回可选项的moussarLabel.last。作为文本视图或标签的text属性,分别接受nil作为值,这不是问题。
#3
0
You should make the array scoped to the class and not the function. In addition a counter variable can be used to access different elements.
您应该使数组作用于类而不是函数。此外,计数器变量可用于访问不同的元素。
To make the text scrollable you can use a UITextView
:
要使文本可滚动,您可以使用UITextView:
@IBOutlet weak var moussarLabel: UITextView!
var moussar = ["dzv","avazv","sdv dv","dvar3"]
var moussarIndex = 0
@IBAction func moussarButton(sender: AnyObject) {
moussarLabel.text = "\(moussar[moussarIndex++])"
// if you want to cycle through the array use the following condition
if moussarIndex >= moussar.count {
moussarIndex = 0
}
}
#1
0
-
The final element in the array is the last thing the loop assigns to the text label. Sounds like its working as expected. Did you mean to concatenate (add) the strings?
数组中的最后一个元素是循环分配给文本标签的最后一个元素。听起来像预期的那样工作。你的意思是连接(添加)字符串?
-
You can do that with a UITextView https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/
您可以使用UITextView执行此操作https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/
#2
0
First of all, you can't scroll a UILabel so you'll have to use a UITextField
. Read the documentation for details, it shouldn't be too hard when only displaying a single word. For getting the last element of the array, use moussarLabel.last
which returns an optional. As the text property of a text view or a label, respectively, accepts nil
as value, this isn't a problem.
首先,您无法滚动UILabel,因此您必须使用UITextField。阅读文档了解详细信息,只显示一个单词时应该不会太难。要获取数组的最后一个元素,请使用返回可选项的moussarLabel.last。作为文本视图或标签的text属性,分别接受nil作为值,这不是问题。
#3
0
You should make the array scoped to the class and not the function. In addition a counter variable can be used to access different elements.
您应该使数组作用于类而不是函数。此外,计数器变量可用于访问不同的元素。
To make the text scrollable you can use a UITextView
:
要使文本可滚动,您可以使用UITextView:
@IBOutlet weak var moussarLabel: UITextView!
var moussar = ["dzv","avazv","sdv dv","dvar3"]
var moussarIndex = 0
@IBAction func moussarButton(sender: AnyObject) {
moussarLabel.text = "\(moussar[moussarIndex++])"
// if you want to cycle through the array use the following condition
if moussarIndex >= moussar.count {
moussarIndex = 0
}
}