我想知道如何使用按钮从数组中删除索引?我可以使用“@IBAction func name(sender:UIButton)”还是我必须使用其他东西?请让我知道,我仍然是一个快速的菜鸟。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelText: UILabel!
@IBOutlet weak var label0: UILabel!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var shoppingList = ["eggs", "milk", "cake", "sugar"]
labelText.text? = "you are missing \(shoppingList.count) items"
if shoppingList.count > 0 {
label0.text = shoppingList[0]
} else {
label0.text = "empty"
}
if shoppingList.count > 1 {
label1.text = shoppingList[1]
} else {
label1.text = "empty"
}
if shoppingList.count > 2 {
label2.text = shoppingList[2]
} else {
label2.text = "empty"
}
if shoppingList.count > 3 {
label3.text = shoppingList[3]
} else {
label3.text = "empty"
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您需要输入要删除的数组中的项目。您可以使用文本字段作为输入,使用按钮进行提交。在这种情况下,您可以使用按钮IBAction作为指示器从文本字段中获取索引。然后使用
删除索引shoppingList.removeAtIndex(indexToBeRemoved)
var shoppingList = ["eggs", "milk", "cake", "sugar"]
应声明为可在成员函数中访问的类的属性。