Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Ios UITAP手势与viewDidLoad/General Tic Tac Toe中的功能通信_Ios_Swift_Uigesturerecognizer - Fatal编程技术网

Ios UITAP手势与viewDidLoad/General Tic Tac Toe中的功能通信

Ios UITAP手势与viewDidLoad/General Tic Tac Toe中的功能通信,ios,swift,uigesturerecognizer,Ios,Swift,Uigesturerecognizer,我一直在尝试使用UILabels和UITapPictures的条件来编程Tic Tac Toe。我觉得我已经理解了大部分逻辑,但我很难理解点击TapGestureRecognitor如何触发viewDidLoad中的函数,因为uilabel文本必须更改 起初,我尝试了viewDidLoad之外的@iActions,但没有成功,然后我尝试了编程连接,但需要#选择器(MyClass.function),但我无法在现有的viewDidLoad函数中实现点击手势识别器的功能 这是我所有的代码 impor

我一直在尝试使用UILabels和UITapPictures的条件来编程Tic Tac Toe。我觉得我已经理解了大部分逻辑,但我很难理解点击TapGestureRecognitor如何触发viewDidLoad中的函数,因为uilabel文本必须更改

起初,我尝试了viewDidLoad之外的@iActions,但没有成功,然后我尝试了编程连接,但需要#选择器(MyClass.function),但我无法在现有的viewDidLoad函数中实现点击手势识别器的功能

这是我所有的代码

import UIKit
class ViewController: UIViewController {
@IBOutlet weak var turnLabel: UILabel!
@IBOutlet weak var box1: UILabel!
@IBOutlet weak var box2: UILabel!
@IBOutlet weak var box3: UILabel!
@IBOutlet weak var box4: UILabel!
@IBOutlet weak var box5: UILabel!
@IBOutlet weak var box6: UILabel!
@IBOutlet weak var box7: UILabel!
@IBOutlet weak var box8: UILabel!
@IBOutlet weak var box9: UILabel!

var openArray = [false, false, false, false, false, false, false, false, false]
var Array1 = [false, false, false, false, false, false, false, false, false]
var Array2 = [false, false, false, false, false, false, false, false, false]
var sampleArray = [false, false, false, false, false, false, false, false, false]
var isWinner = false
var player = 1

override func viewDidLoad()
{
    super.viewDidLoad()

     var labelsArray = [box1,box2,box3,box4,box5,box6,box7,box8,box9]
    turnLabel.text = "Player \(player)'s turn"

    var tap1 = UIGestureRecognizer(target: self, action: #selector(ViewController.viewDidLoad.box1tapped))
    self.box1.addGestureRecognizer(tap1)
    self.box1.userInteractionEnabled = true
    //tap1.numberOfTapsRequired = 1
    //tap1.numberOfTouchesRequired = 1 wasn't sure if I needed these
    var tap2 = UIGestureRecognizer(target: self, action: "box2tapped:")
    self.box2.addGestureRecognizer(tap1)
    self.box2.userInteractionEnabled = true
    var tap3 = UIGestureRecognizer(target: self, action: "box3tapped:")
    self.box3.addGestureRecognizer(tap1)
    self.box3.userInteractionEnabled = true
    var tap4 = UIGestureRecognizer(target: self, action: "box4tapped:")
    self.box4.addGestureRecognizer(tap1)
    self.box4.userInteractionEnabled = true
    var tap5 = UIGestureRecognizer(target: self, action: "box5tapped:")
    self.box5.addGestureRecognizer(tap1)
    self.box5.userInteractionEnabled = true
    var tap6 = UIGestureRecognizer(target: self, action: "box6tapped:")
    self.box6.addGestureRecognizer(tap1)
    self.box6.userInteractionEnabled = true
    var tap7 = UIGestureRecognizer(target: self, action: "box7tapped:")
    self.box7.addGestureRecognizer(tap1)
    self.box7.userInteractionEnabled = true
    var tap8 = UIGestureRecognizer(target: self, action: "box8tapped:")
    self.box8.addGestureRecognizer(tap1)
    self.box8.userInteractionEnabled = true
    var tap9 = UIGestureRecognizer(target: self, action: "box9tapped:")
    self.box9.addGestureRecognizer(tap1)
    self.box9.userInteractionEnabled = true

        func box1tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[0] = true
            whichPlayer(Box: 0)
            self.box1.userInteractionEnabled = false
        }
        func box2tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[1] = true
            whichPlayer(Box: 1)
            self.box2.userInteractionEnabled = false
        }
        func box3tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[2] = true
            whichPlayer(Box: 2)
            self.box3.userInteractionEnabled = false
        }
        func box4tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[3] = true
            whichPlayer(Box: 3)
            self.box4.userInteractionEnabled = false
        }
        func box5tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[4] = true
            whichPlayer(Box: 4)
            self.box5.userInteractionEnabled = false
        }
        func box6tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[5] = true
            whichPlayer(Box: 5)
            self.box6.userInteractionEnabled = false
        }
        func box7tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[6] = true
            whichPlayer(Box: 6)
            self.box7.userInteractionEnabled = false
        }
        func box8tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[7] = true
            whichPlayer(Box: 7)
            self.box8.userInteractionEnabled = false
        }
        func box9tapped(recognizer: UITapGestureRecognizer)
        {
            openArray[8] = true
            whichPlayer(Box: 8)
            self.box9.userInteractionEnabled = false
        }
    func whichPlayer(Box i:Int)
    {
        if player == 1
        {
            Array1[i] = true
            labelsArray[i].text = "X"
            sampleArray = Array1
            isWinner = isWin()
            if(isWinner == true)
            {
                popAlert()
            }
            player = 2
            turnLabel.text = "Player \(player)'s turn"
        }
        if player == 2
        {
            Array2[i] = true
            labelsArray[i].text = "O"
            sampleArray = Array2
            isWinner = isWin()
            if(isWinner == true)
            {
                popAlert()
            }
            player = 1
            turnLabel.text = "Player \(player)'s turn"
        }
    }
    func isWin() -> Bool
    {
        if(sampleArray[0]==true && sampleArray[3]==true && sampleArray[6]==true)
        {return true}
        else if(sampleArray[1]==true && sampleArray[4]==true && sampleArray[7]==true)
        {return true}
        else if(sampleArray[2]==true && sampleArray[5]==true && sampleArray[8]==true)
        {return true}
        else if(sampleArray[0]==true && sampleArray[1]==true && sampleArray[2]==true)
        {return true}
        else if(sampleArray[3]==true && sampleArray[4]==true && sampleArray[5]==true)
        {return true}
        else if(sampleArray[6]==true && sampleArray[7]==true && sampleArray[8]==true)
        {return true}
        else if(sampleArray[2]==true && sampleArray[4]==true && sampleArray[6]==true)
        {return true}
        else if(sampleArray[0]==true && sampleArray[4]==true && sampleArray[8]==true)
        {return true}
        else
        {return false}
    }
    func popAlert()
    {
        let alert = UIAlertController(title: "Alert", message: nil, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Ok", style: .Cancel, handler: { (UIAlertAction) in

        })
        let playAgain = UIAlertAction(title: "Play Again", style: .Default, handler: { (UIAlertAction) in
            var i = 1
            while i<10
            {
              labelsArray[i].text = ""
              self.openArray[i] = false
              self.Array1[i] = false
              self.Array2[i] = false
              i += 1
            }
            self.isWinner = false
        })

        if(isWinner)
        {
            alert.addAction(okAction)
            alert.addAction(playAgain)
            presentViewController(alert, animated: true, completion: nil)
        }
    }
}// End viewDidLoad    

您不应创建
UIgestureRecognitizer
的实例。仅创建特定类型手势的实例,如
UITapgestureRecognitizer
。并了解有关数组和插座集合的知识。有9个标签插座很笨重。为什么要使用可点击标签?为什么不使用
ui按钮
?最重要的是,不要以这种方式使用嵌套函数。虽然斯威夫特支持这一点,但至少在我的经验中,这很少被使用。您应该将点击处理程序移到
viewDidLoad
之外。您说过
iAction
s对您不起作用-他们应该这样做。这种方法出了什么问题?您永远不应该创建
UIGestureRecognizer
的实例。仅创建特定类型手势的实例,如
UITapgestureRecognitizer
。并了解有关数组和插座集合的知识。有9个标签插座很笨重。为什么要使用可点击标签?为什么不使用
ui按钮
?最重要的是,不要以这种方式使用嵌套函数。虽然斯威夫特支持这一点,但至少在我的经验中,这很少被使用。您应该将点击处理程序移到
viewDidLoad
之外。您说过
iAction
s对您不起作用-他们应该这样做。这种方法出了什么问题?
var tap1 = UIGestureRecognizer(target: self, action: #selector(ViewController.viewDidLoad.box1tapped))