0
如何在Swift中遍歷對象/變量。附加對象時,我可以創建數組還是字典,以便我不必爲每個按鈕編寫代碼。我是一個nubie,所以請告訴我像我四歲。我在這裏先向您的幫助表示感謝。遍歷對象/變量Swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
...
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.button1.alpha = 0.0
self.button2.alpha = 0.0
self.button3.alpha = 0.0
...
}
是否可以將變量添加到對象名稱中。例如:button /(count).alpha = 0.0 –
@codekid,我不認爲你可以添加一個像這樣的變量,一旦一個對象被創建。但如果你想調用一個特定的按鈕,你可以創建一個字典'[String:UIButton]'而不是數組''「button1」:button1,「button2」:button2,「button3」:button3]'。然後調用'dictOfButtons [「button /(count)」]。alpha = 0.0'。 – Tim