Arrays UIButton矩阵-无法正确显示
我想要一个9x92D的UIButtons数组(矩阵)。 这是我的代码:Arrays UIButton矩阵-无法正确显示,arrays,swift,xcode,uibutton,Arrays,Swift,Xcode,Uibutton,我想要一个9x92D的UIButtons数组(矩阵)。 这是我的代码: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. var x = 0 var y = 0
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var x = 0
var y = 0
while(x<10){
while(y<10){
let button = UIButton(frame: CGRect(x: (x+1)*50, y: (y+1)*50, width: 50, height: 50))
button.backgroundColor = .green
button.setTitle("(" + String(x) + ", " + String(y) + ")", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
y+=1
}
x+=1
}
}
@objc func buttonAction(sender: UIButton!) {
print("Button " + sender.title(for: .normal)! + " tapped")
}
}
导入UIKit
类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
变量x=0
变量y=0
而(xvar x=0
while(x)忽略按钮。只需打印x和打印y。您将看到。您需要“重置”y。可以在x+=1
行之后执行,然后您需要y=0
,或者使用相同的逻辑,将var y=0
,放在while y之前。
var x = 0
while(x<10){
var y = 0
while(y<10){
let button = UIButton(frame: CGRect(x: (x+1)*50, y: (y+1)*50, width: 50, height: 50))
button.backgroundColor = .green
button.setTitle("(" + String(x) + ", " + String(y) + ")", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
y+=1
}
x+=1
}