Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays UIButton矩阵-无法正确显示_Arrays_Swift_Xcode_Uibutton - Fatal编程技术网

Arrays UIButton矩阵-无法正确显示

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

我想要一个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
        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
而(x
var 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
        }