ios - TicTacToe如何重置containerView内容?

标签 ios swift

我在这里需要一些帮助。我尝试学习...一些东西并进行练习,但我无法管理该功能。

我有这个函数来创建容器 View :

    func createGameView() {
    let containerView = UIView()
    containerView.frame = CGRect(x: 0,
                                 y: self.screenHeight * 0.4,
                                 width: screenWidth,
                                 height: screenHeight * 0.6)

    containerView.backgroundColor = UIColor(red:0.99, green:0.13, blue:0.00, alpha:1)
    self.view.addSubview(containerView)

之后我有一个函数将用 9 个方 block 填充容器 View

    func createGridView( _ container : UIView) {

    let buttonWidth = container.frame.size.width / 3

    let buttonHeight = container.frame.size.height / 3

    for i in [0,1,2] {
        for j in 0...2 {
        let button = UIButton(frame:
        CGRect( x : buttonWidth * CGFloat (i),
                y :buttonHeight * CGFloat (j),
                width : buttonWidth,
                height : buttonHeight ))
    button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
    button.layer.borderColor = UIColor.black.cgColor
    button.layer.borderWidth = 1.0
        container.addSubview(button)
            button.addTarget(self, action: #selector(GameViewController.addGameValue(sender:)), for: .touchUpInside)
            button.tag = i + 3 * j
            self.gameButtons.append(button)
      }
    }
}

我有一个名为“重置”的按钮

    var resetButton = UIButton(frame:
        CGRect(x: Double(containerView.frame.size.width  - 100),
               y: Double(containerView.frame.size.height - 60),
               width: 60,
               height: 40.0)
        )

    resetButton.setTitle ("Reset", for: UIControlState.normal)
    resetButton.setTitleColor(UIColor.black, for: UIControlState.normal)
    resetButton.setTitleColor(UIColor.white, for: .highlighted)
    resetButton.layer.borderWidth = 1.0
    resetButton.layer.borderColor = UIColor.black.cgColor
    resetButton.layer.cornerRadius = 5.0

    resetButton.addTarget(self, action: #selector(GameViewController.resetGame), for: UIControlEvents.touchUpInside)

    containerView.addSubview(resetButton)

这就是我遇到问题的地方:(

func resetGame (sender: UIButton) {  
    // .addTarget(self, action: #selector(self.createGameView), for: .touchUpInside)
   print(" reset me ")
   // .addTarget(self, action: #selector(GameViewController.resetGame), for: .touchUpInside)

}

任何人都可以告诉我为什么这些都不能重置该值?是一个井字棋游戏,在放置一些 x 和 0 之后我想将 gridView 恢复到初始状态。不知道该怎么做。

最佳答案

在调用 createGridView 后,您已经拥有了需要添加到 View 中的所有按钮,因此在重置时,您所要做的就是重置按钮状态,清除所有分数/分数和您的准备重新开始。

func resetGame() {
   // reset any scores, colors etc
   for button in self.gameButtons {
       button.setTitle ("", for: UIControlState.normal)
       button.backgroundColor = UIColor.init(colorLiteralRed: 0.1, green: 0.89, blue: 0.05, alpha: 1)
   }
}

关于ios - TicTacToe如何重置containerView内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879460/

相关文章:

ios - FDT 无法为 iOS 编译 "compile-abc: Permission denied"

ios - 为什么我得到 "Permission Denied"

swift - 通过 rightCalloutAccessoryView 访问自定义 MKAnnotation 数据时出现问题

ios - 单元格洞察 UICollectionView 在可见之前不加载?

ios - 当 App 在前台 Swift 4 iOS 11 时获取本地通知

ios - EasyAPNS 配置

ios - UIApplication.shared.applicationIconBadgeNumber 滞后

ios - ERROR ITMS-9000 : "This bundle is invalid. IPA 格式需要一个名为 Payload 的顶级目录,仅包含一个 .app 包和可选的

ios - 呈现 UIView 的最少代码

ios - 获得最高可用的 AVCaptureSessionPreset