UIViewController跳转到另一个Controller失败

时间:2015-12-11 12:36:20

标签: swift uiviewcontroller

 import UIKit
 var map = [Int](count: 81, repeatedValue: 0)
 var allPoint = [[Point]]()
 var allButton = [[UIButton]]()
 var cat = Point(row: 4, col: 4)

class gameController: UIViewController {
var catImageView:UIImageView!
override func viewDidLoad()
{
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.blackColor()
    let bgImage = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
    bgImage.image = UIImage(named: "bg.png")
    self.view.addSubview(bgImage)
    constructButton()
    constructCat()
    constructRandomWall(false)
}
func constructButton()
{
    for row in 0...8
    {
        var rowButton = [UIButton]()
        var button = UIButton()
        for col in 0...8
        {

            if row % 2 == 0
            {
                button = UIButton(frame: CGRectMake(CGFloat(45+30*col), CGFloat(250+35*row), CGFloat(30), CGFloat(30)))
            }else
            {
                button = UIButton(frame: CGRectMake(CGFloat(60+30*col), CGFloat(250+35*row), CGFloat(30), CGFloat(30)))
            }
            button.addTarget(self, action: Selector("ClickButton:"), forControlEvents: .TouchUpInside)
            button.setImage(UIImage(named: "unClick.png"), forState: .Normal)
            self.view.addSubview(button)
            rowButton.append(button)
        }
        allButton.append(rowButton)
    }
}

func constructCat()
{
    catImageView = UIImageView(frame: CGRectMake(CGFloat(45+30*4), CGFloat(250+35*3), CGFloat(30), CGFloat(60)))
    catImageView.image = UIImage(named:"Cat.png")
    catImageView.animationImages = (1...3).map({return UIImage(named: "cat\($0).png")!})
    catImageView.animationDuration = NSTimeInterval(1.0)
    catImageView.startAnimating()
    self.view.addSubview(catImageView)
}

func constructRandomWall(isAgain:Bool)
{
    var countWall = 0
    let limit: UInt32 = 30
    let result = Int(arc4random() % limit) + 10
    initMarkMap(isAgain)
    var i = 0,j = 0
    while(countWall < result)
    {
        i = Int(arc4random() % 9)
        j = Int(arc4random() % 9)
        if i == 4 && j == 4
        {
            continue
        }
        if  map[i*9+j] == 0
        {
            map[i*9+j] = 1
            allButton[i][j].setImage(UIImage(named: "Click.png"), forState: .Normal)
            allButton[i][j].enabled = false
            ++countWall
        }
    }
    buildPath()
}

func initMarkMap(isAgain:Bool)
{
    for index in 0...80
    {
        map[index] = 0
    }
    constructPointToButton(isAgain)
}

func constructPointToButton(isAgain:Bool)
{
    if isAgain
    {
        for i in 0...8
        {
            for j in 0...8
            {
                allPoint[i][j].cost = 82
                allPoint[i][j].connection = 0
            }
        }
    }
    else
    {
        for i in 0...8
        {
            var rowPoint = [Point]()
            for j in 0...8
            {
                let point = Point(row: i, col: j)
                point.cost = 82
                point.connection = 0
                rowPoint.append(point)
            }
            allPoint.append(rowPoint)
        }
    }

}
func buildPath()
{
    //左上
    for i in 0...8
    {    for j in 0...8
    {
        allPoint[j][i].constructMinPath()
        allPoint[i][j].constructMinPath()

        }
    }

    //左下
    for i in 0...8
    {    for j in 0...8
    {
        allPoint[8-i][j].constructMinPath()
        allPoint[8-j][i].constructMinPath()
        }
    }

    //右上
    for i in 0...8
    {    for j in 0...8
    {
        allPoint[i][8-j].constructMinPath()
        allPoint[j][8-i].constructMinPath()
        }
    }

    //右下
    for i in 0...8
    {
        for j in 0...8
        {
            allPoint[8-i][8-j].constructMinPath()
            allPoint[8-j][8-i].constructMinPath()
        }
    }
}

func ClickButton(sender:UIButton)
{
    sender.setImage(UIImage(named: "Click.png"), forState: .Normal)
    sender.enabled = false
    start()
}

func start()
{
    let getDirection = getGoLocation()
    //show()
    //print("row = \(getDieection!.row) col = \(getDieection!.col)")
    let row = getDirection.row
    let col = getDirection.col
    cat.row = row
    cat.col = col
    if row % 2 == 0
    {
        catImageView.frame = CGRectMake(CGFloat(45+30*col), CGFloat(250+35*(row-1)), CGFloat(30), CGFloat(60))
    }
    else
    {
        catImageView.frame = CGRectMake(CGFloat(60+30*col), CGFloat(250+35*(row-1)), CGFloat(30), CGFloat(60))
    }
    if cat.judgeInBorder()
    {
        end(0)
    }
    else if cat.conNotGo()
    {
        end(1)
    }

}

func getGoLocation() -> Point
{
    buildPath()
    let canGo = cat.getAllenbalePoint()
    var p:Point = cat
    var min = 8200
    if canGo.count > 0
    {
        for value in canGo
        {
            let temp = (value?.cost)!
            print("temp = \(temp)");
            if(min > temp)
            {
                min = temp
                p = value!
                print("min = \(temp)");
            }
            else if min == temp
            {
                if p.connection < value!.connection
                {
                    min = (value?.cost)!
                    p = value!
                }
            }
        }
        print("最后走的点row = \(p.row)  col = \(p.col))", separator: "", terminator: "\n")
        return p
    }
    else
    {
        return p
    }
}

func end(result:Int)
{
    var alert:UIAlertController!
    if result == 0
    {
        alert = UIAlertController(title: "游戏结束", message: "你输了", preferredStyle: .Alert)
    }
    else
    {
        alert = UIAlertController(title: "游戏结束", message: "你赢了", preferredStyle: .Alert)
    }
    let action1 = UIAlertAction(title: "退出游戏", style: .Default, handler: {
        (ac:UIAlertAction) in self.DidExit()
    })
    let action2 = UIAlertAction(title: "重来一次", style: .Default, handler: {
        (ac:UIAlertAction) in self.startAgain()
    })
    alert.addAction(action1)
    alert.addAction(action2)
    self.presentViewController(alert, animated: true, completion: nil)
}

func startAgain()
{
    //Button
    for row in 0...8
    {
        for col in 0...8
        {
            allButton[row][col].setImage(UIImage(named: "unClick.png"), forState: .Normal)
            allButton[row][col].enabled = true
        }
    }
    //Wall
    constructRandomWall(true)
    //Cat
    catImageView.frame = CGRectMake(CGFloat(45+30*4), CGFloat(250+35*3), CGFloat(30), CGFloat(60))
    cat.row = 4
    cat.col = 4
}
//    func show()
//    {
//        for var i = 0 ;i<=8 ;++i
//        {
//            for var j = 0; j <= 8; ++j
//            {
//                print(allPoint[i][j].cost, separator: "", terminator: "\t")
//            }
//            print("", separator: "", terminator: "\n")
//        }
//    }

func DidExit()
{
    exit(0)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

import UIKit

class showView: UIViewController {
var number:Int
var image:UIImageView!
init(number:Int)
{
    self.number = number
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad()
{
    super.viewDidLoad()
    let image = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
        image.image = UIImage(named: "start\(number)")
    if number == 2
    {
        let button = UIButton(frame: CGRect(x: self.view.frame.width/3-10,y: self.view.frame.height-180,width: self.view.frame.width/3+20,height: 70))
            button.addTarget(self, action: Selector("startGame"), forControlEvents: UIControlEvents.TouchUpInside)
            button.setTitle("StartGame", forState: .Normal)
            button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        self.view.addSubview(image)
        self.view.addSubview(button)
    }
    else
    {
        self.view.addSubview(image)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}
import UIKit

class showView: UIViewController {
var number:Int
var image:UIImageView!
init(number:Int)
{
    self.number = number
    super.init(nibName: nil, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad()
{
    super.viewDidLoad()
    let image = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
        image.image = UIImage(named: "start\(number)")
    if number == 2
    {
        let button = UIButton(frame: CGRect(x: self.view.frame.width/3-10,y: self.view.frame.height-180,width: self.view.frame.width/3+20,height: 70))
            button.addTarget(self, action: Selector("startGame"), forControlEvents: UIControlEvents.TouchUpInside)
            button.setTitle("StartGame", forState: .Normal)
            button.setTitleColor(UIColor.blueColor(), forState: .Normal)
        self.view.addSubview(image)
        self.view.addSubview(button)
    }
    else
    {
        self.view.addSubview(image)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

以上是我的代码部分,我尝试在ViewController中跳转到gameController,在代码值中等于true,但它不执行跳转可以帮助我吗?

enter image description here enter image description here

0 个答案:

没有答案