Swift - 在点击按钮时添加和删除 UILabel
Swift - Add and Remove UILabel on button tap
我创建了两个按钮,一个用于创建 UILabel,另一个用于删除创建的 UILabel。
在点击 Button1 时,我是 运行 一个 for 循环,以创建四 (4) 个 UILabel,我能够做到这一点。
点击 Button2 时,我想删除使用 Button1 创建的所有 UILabel。
旁注:我不想隐藏 UILabel,因为变量 'noOfLabels' 可以根据需要从 4 增加到 15 或任何其他数字。
这是我试过的方法。
class ViewController: UIViewController {
var myLabel : UILabel!
var noOfLabels = 4
@IBAction func addButton(_ sender: Any) {
if(myLabel != nil && !myLabel.isHidden)
{
myLabel.removeFromSuperview()
}
print("AddLabel button is Tapped")
var yval = 0
for i in 0...noOfLabels
{
myLabel = UILabel()
myLabel.frame = CGRect(x: 30, y: 200 + yval, width: 90, height: 50)
myLabel.text = "Hello \(i)"
view.addSubview(myLabel)
yval += 80
}
}
@IBAction func removeButton(_ sender: Any) {
print("Remove button is Tapped")
myLabel.removeFromSuperview()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
使用上面的代码,我只能删除一个标签。
我需要帮助删除点击 Button2 时的所有 UILabel
发生这种情况是因为您只引用了最后创建的标签实例,而您只删除了这个实例。您需要标签数组才能删除所有创建的标签。
var labels = [UILabel]()
然后当您需要创建新标签时,也将其附加到此数组
@IBAction func addButton(_ sender: Any) {
labels.forEach { [=11=].removeFromSuperview() }
labels.removeAll()
for i in 0...noOfLabels {
let newLabel = UILabel(frame: CGRect(x: 30, y: 200 + (i * 80), width: 90, height: 50)) // <--- new instance
newLabel.text = "Hello \(i)"
view.addSubview(newLabel)
labels.append(newLabel) // <--- appending to an array
}
}
然后当你需要从它们的父视图中删除所有标签时,只需遍历 labels
数组
@IBAction func removeButton(_ sender: Any) {
labels.forEach { [=12=].removeFromSuperview() }
labels.removeAll()
}
定义
var labelList: [Label] = []
在
addButton(_ sender: Any) {
...
view.addSubview(myLabel)
labelList.append(myLabel)
...
}
@IBAction func removeButton(_ sender: Any) {
print("Remove button is Tapped")
for i in 0...noOfLabels
{
labelList[i].removeFromSuperview()
}
labelList = []
我创建了两个按钮,一个用于创建 UILabel,另一个用于删除创建的 UILabel。
在点击 Button1 时,我是 运行 一个 for 循环,以创建四 (4) 个 UILabel,我能够做到这一点。
点击 Button2 时,我想删除使用 Button1 创建的所有 UILabel。
旁注:我不想隐藏 UILabel,因为变量 'noOfLabels' 可以根据需要从 4 增加到 15 或任何其他数字。
这是我试过的方法。
class ViewController: UIViewController {
var myLabel : UILabel!
var noOfLabels = 4
@IBAction func addButton(_ sender: Any) {
if(myLabel != nil && !myLabel.isHidden)
{
myLabel.removeFromSuperview()
}
print("AddLabel button is Tapped")
var yval = 0
for i in 0...noOfLabels
{
myLabel = UILabel()
myLabel.frame = CGRect(x: 30, y: 200 + yval, width: 90, height: 50)
myLabel.text = "Hello \(i)"
view.addSubview(myLabel)
yval += 80
}
}
@IBAction func removeButton(_ sender: Any) {
print("Remove button is Tapped")
myLabel.removeFromSuperview()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
使用上面的代码,我只能删除一个标签。
我需要帮助删除点击 Button2 时的所有 UILabel
发生这种情况是因为您只引用了最后创建的标签实例,而您只删除了这个实例。您需要标签数组才能删除所有创建的标签。
var labels = [UILabel]()
然后当您需要创建新标签时,也将其附加到此数组
@IBAction func addButton(_ sender: Any) {
labels.forEach { [=11=].removeFromSuperview() }
labels.removeAll()
for i in 0...noOfLabels {
let newLabel = UILabel(frame: CGRect(x: 30, y: 200 + (i * 80), width: 90, height: 50)) // <--- new instance
newLabel.text = "Hello \(i)"
view.addSubview(newLabel)
labels.append(newLabel) // <--- appending to an array
}
}
然后当你需要从它们的父视图中删除所有标签时,只需遍历 labels
数组
@IBAction func removeButton(_ sender: Any) {
labels.forEach { [=12=].removeFromSuperview() }
labels.removeAll()
}
定义
var labelList: [Label] = []
在
addButton(_ sender: Any) {
...
view.addSubview(myLabel)
labelList.append(myLabel)
...
}
@IBAction func removeButton(_ sender: Any) {
print("Remove button is Tapped")
for i in 0...noOfLabels
{
labelList[i].removeFromSuperview()
}
labelList = []