在项目已经关闭时关闭项目检查状态
turning items chck state off when they are already turned of
我对编程有一定的了解
假设有
3 个菜单项,其中一个被选中
当我点击另一个时我需要它
它取消选中当前选择的
说起来容易
关闭除刚被点击的那个以外的所有内容
但你最终 运行 一个函数
关闭已经关闭的项目
这是一个普遍的问题吗?
这会占用太多资源吗?
我看不到任何其他不关闭的方法
已经关闭的东西
在设置一项之前先关闭每一项不是问题。
但是,如果您仍然只想处理选中的项目,可以使用条件 for 循环或映射来完成。
这是 for 循环示例:
//Class representing the item.
class Item {
var isChecked = false
}
//Array of 3 items.
var items: [Item] = [Item(), Item(), Item()]
//Function that should be called (as @IBAction) when an item is tapped.
func itemIsTapped(itemTag: Int) {
selectItemAt(index: itemTag)
}
//Function that unselected the checked item and select the one that should be.
func selectItemAt(index: Int) {
//This is the part where you UNCHECKED ONLY the CHECKED item.
for item in items where item.isChecked {
item.isChecked = false
}
items[index].isChecked = true
}
我对编程有一定的了解
假设有 3 个菜单项,其中一个被选中 当我点击另一个时我需要它 它取消选中当前选择的
说起来容易
关闭除刚被点击的那个以外的所有内容
但你最终 运行 一个函数 关闭已经关闭的项目
这是一个普遍的问题吗? 这会占用太多资源吗? 我看不到任何其他不关闭的方法 已经关闭的东西
在设置一项之前先关闭每一项不是问题。
但是,如果您仍然只想处理选中的项目,可以使用条件 for 循环或映射来完成。
这是 for 循环示例:
//Class representing the item.
class Item {
var isChecked = false
}
//Array of 3 items.
var items: [Item] = [Item(), Item(), Item()]
//Function that should be called (as @IBAction) when an item is tapped.
func itemIsTapped(itemTag: Int) {
selectItemAt(index: itemTag)
}
//Function that unselected the checked item and select the one that should be.
func selectItemAt(index: Int) {
//This is the part where you UNCHECKED ONLY the CHECKED item.
for item in items where item.isChecked {
item.isChecked = false
}
items[index].isChecked = true
}