Ios 如果标题文本为空或未指定,如何隐藏uibutton?
代码和问题代表Swift中的一个Xcode项目。我有一组按钮,根据标签上显示的文本显示选项 标签文本来自字典的键,按钮文本来自同一字典的值。字典类型为Ios 如果标题文本为空或未指定,如何隐藏uibutton?,ios,swift,if-statement,uibutton,Ios,Swift,If Statement,Uibutton,代码和问题代表Swift中的一个Xcode项目。我有一组按钮,根据标签上显示的文本显示选项 标签文本来自字典的键,按钮文本来自同一字典的值。字典类型为[String:[String]。键和值都放在数组中。我当前显示的数据正确,但某些值的长度不同于其他值 例如,一个键有3个值,另一个键有5个值。如果没有要发送的文本,我想隐藏按钮。因此,如果一个键显示在标签中,并且有3个值,我只想显示3个按钮,依此类推。实现此功能的最佳方法是什么?下面是我的代码,它没有实现我想实现的目标: func startSu
[String:[String]
。键和值都放在数组中。我当前显示的数据正确,但某些值的长度不同于其他值
例如,一个键有3个值,另一个键有5个值。如果没有要发送的文本,我想隐藏按钮。因此,如果一个键显示在标签中,并且有3个值,我只想显示3个按钮,依此类推。实现此功能的最佳方法是什么?下面是我的代码,它没有实现我想实现的目标:
func startSurvey() {
if surveyQuestions.isEmpty {
surveyQuestions = Array(SampleSurvey().surveyquestions.keys)
print(surveyQuestions)
}
let rand = Int(arc4random_uniform(UInt32(surveyQuestions.count)))
questionTitle.text = surveyQuestions[rand]
var choices = SampleSurvey().surveyquestions[surveyQuestions[rand]]!
print(choices)
print(choices.count)
surveyQuestions.remove(at: rand)
var button = UIButton()
var x = 0
// var choicePool = choices.count
if choices.count == 2 {
for index in 1...2 {
button = view.viewWithTag(index) as! UIButton
button.setTitle(choices[x], for: .normal)
x += 1
if button.titleLabel?.text.isEmpty == true {
button.isHidden = true
}
}
}
else if choices.count == 4 {
for index in 1...4 {
button = view.viewWithTag(index) as! UIButton
button.setTitle(choices[x], for: .normal)
x += 1
if button.titleLabel?.text.isEmpty == true {
button.isHidden = true
}
}
}
这是模拟器的屏幕截图,你可以看到这个特殊的键只有2个值,所以有3个空白按钮,我想隐藏空白的按钮:
更新:以下代码已授予我目标功能:
var button = UIButton()
var x = 0
let buttonTags = [0,1,2,3,4]
if choices.count == 2 {
for idx in buttonTags {
button = surveyChoices[idx]
if idx < choices.count {
button.setTitle(choices[x], for: .normal)
x += 1
} else {
button.isHidden = true
}
}
}
var-button=UIButton()
变量x=0
设buttonTags=[0,1,2,3,4]
如果choices.count==2{
对于按钮标记中的idx{
按钮=测量选择[idx]
如果idx
您可以尝试以下方法:
SWIFT 4
button.ishiden=button.titleLabel?.text==nil | | button.titleLabel?.text==
就个人而言,我会动态添加按钮,调查中的每个选项对应一个按钮。通过添加包含按钮(tableView或collectionView)的行/单元格,您可以使用UITableView
、UICollectionView
或UIStackView
中的任意一个按钮轻松完成此操作或者只需在垂直堆栈视图中添加一个按钮
对于您的特定代码,您只需迭代选择的数量,因此在下面的示例中(来自您的代码),您只需操作两个按钮,而不接触其他按钮
if choices.count == 2 {
for index in 1...2 {
button = view.viewWithTag(index) as! UIButton
button.setTitle(choices[x], for: .normal)
x += 1
if button.titleLabel?.text.isEmpty == true {
button.isHidden = true
}
}
}
对于1…2中的索引
…您没有对其他3执行任何操作
您应该循环浏览所有按钮,如果有选择,请设置标题,否则隐藏按钮
下面是一个在操场上工作的示例:
let question = ["are you expecting a child": ["yes", "no"]]
let choices = question["are you expecting a child"]!
let buttonTags = [0, 1, 2, 3, 4]
let buttons = [
UIButton(),
UIButton(),
UIButton(),
UIButton(),
UIButton()
]
for idx in buttonTags {
let button = buttons[idx]
if idx < choices.count {
button.setTitle(choices[idx], for: .normal)
} else {
button.isHidden = true
}
}
buttons.map {
print($0.titleLabel?.text)
print($0.isHidden)
}
如果按钮标题标签中的
文本为nil
,则您的条件将变为false。
试着像这样修改代码:
用于1…4中的索引{
button=视图。viewWithTag(索引)为!UIButton
button.setTitle(选项[x],用于:。正常)
x+=1
if(button.titleLabel?.text???).isEmpty==true{
button.ishiden=true
}
}
这将检查文本是否为nil
,然后返回“”
其中为空。您可以使用string.i空进行检查。尝试过的string.i空按钮仍会出现空白按钮。您确定要添加带有正确标签的按钮吗?这可以改进很多,如果您使用stackview,您的调查问题应该定义可能的重复,然后为每个重复动态创建一个UIControl,可以是按钮、开关、滑块等。是的标记为1、2、3、4、5。我已检查并双重检查。@SeanLintern88按钮在StackView中仍然不工作。按钮仍然显示为空白
Optional("yes")
false
Optional("no")
false
nil
true
nil
true
nil
true