iOS开发 Swift开发数独游戏(四) 游戏界面的界面与逻辑

一、游戏界面涉及到的功能点

1)数独格子的建模

(1)绘制数独格子要考虑到标记功能

所以要在每个格子内预先塞入9个标记数字,仅数独格子算下来就有9*9*9=729个格子且存在大量嵌套(这导致我在操作Storyboard时每一个修改都要等待至少20s)

(2)数独格子内部的Tag标记

为了方便编程时获取相应格子的View,需要把每个格子打上Tag

(3)数独格子的初始化

模型初始化包括背景色、填数操作 和 边框粗细的调整

2)“选数视图”与“选标记视图”以及“其他按钮”的建模

3)填数的游戏游戏规则的实现

(1)判断一个格子能填什么数

(2)判断游戏是否成功结束

二、具体实现

1)9*9=81个数独格子的创建。

(0)我们先在Storyboard中建一个viewcontroller,取名为Game。再在xcode左边新建一个Swift类,取名GameViewController,继承ViewController。将Storyboard中的Game属性中的父类设置成刚刚创建的GameViewController。

(1)我们称一个可以填数独数字的格子为UnitCell

涉及的代码:

   1:  import Foundation
   2:  import UIKit
   3:  class UnitCell: UIButton {
   4:      var numberAvailable = true
   5:      var superDelegate:SNNUnit?
   6:      // MARK: - 隐藏所有标记
   7:      func hideAllMarks(){
   8:          for view:UILabel in self.subviews as [UILabel]{
   9:             view.hidden=true
  10:          }
  11:      }
  12:      // MARK: - 添加标记
  13:      func addNumberMark(i:Int){
  14:          hideNumberBig()
  15:          for view:UILabel in self.subviews as [UILabel]{
  16:              if view.text == String(i){
  17:                  if view.hidden {
  18:                      view.hidden = false
  19:                  }
  20:  //                else{
  21:  //                    view.hidden = true
  22:  //                }
  23:                  break
  24:              }
  25:          }
  26:      }
  27:      // MARK: - 锁定单元格
  28:      func lockUnit(){
  29:          self.enabled = false
  30:          self.backgroundColor = UIColor.yellowColor()
  31:      }
  32:      // MARK: - 设置特定的数字
  33:      func setCertainNumberBig(v:Int){
  34:          hideAllMarks()
  35:          self.setTitle(String(v), forState: UIControlState.Normal)
  36:          self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
  37:      }
  38:      // MARK: - 隐藏数字
  39:      func hideNumberBig(){
  40:          self.setTitle("", forState: UIControlState.Normal)
  41:      }
  42:      // MARK: - 初始化单元格
  43:      func initCells(){
  44:          hideAllMarks()
  45:          hideNumberBig()
  46:          self.enabled=true
  47:          self.layer.borderWidth=1
  48:          self.backgroundColor = UIColor.whiteColor()
  49:          self.titleLabel?.text = ""
  50:          self.titleLabel?.font = UIFont.systemFontOfSize(UIFont.systemFontSize()+25.0)
  51:      }
  52:      
  53:  }

在xib或storyboard中绘制格子,设置父类为UnitCell

Screen Shot 2015-03-05 at 5.32.35 PM

具体结构为一个UnitCell内部包含9个label,一个子label Size10*21,UnitCell长宽 60*60。

然后创建点击这个UnitCell时执行的动作,

Screen Shot 2015-03-06 at 9.44.04 AM

如图,选择中间那个两个圈圈的视图,右击我们刚刚创建好的UnitCell,将该UnitCell的TouchUpInside指向GameViewController中的空白处,如图

Screen Shot 2015-03-06 at 9.47.40 AM松开后弹出对话框,为事件取名,我们把他取名为onUnitCellTouch

具体里面写什么现在不着急,后面才会用到。之所以现在就设置,是因为一会要复制这个UnitCell,复制新的UnitCell,你创建的点击事件关系也会一起复制过去。避免麻烦。

(2)我们称能同时显示9个格子的单元格为S9Unit,

   1:  //
   2:  //  S9Unit.swift
   3:  //  sudoku-v02
   4:  //
   5:  //  Created by 张泽阳 on 2/14/15.
   6:  //  Copyright (c) 2015 张泽阳. All rights reserved.
   7:  //
   8:   
   9:  import Foundation
  10:  import UIKit
  11:  /**
  12:      包含9个单元格(UnitCell)的九宫格
  13:  */
  14:  class S9Unit: UIView {
  15:      // MARK: - 初始化单元格
  16:      func initAllCells(){
  17:  //        self.layer.borderWidth=2
  18:          for unit:UnitCell in self.subviews as [UnitCell]{
  19:  //            println("S9's subviews:\(unit.description)\n\(unit.tag)")
  20:              unit.initCells()
  21:          }
  22:          self.layer.borderWidth=2
  23:      }
  24:      // MARK: - 取得指定tag单元格
  25:      func getTheUnit(j:Int) ->UnitCell{
  26:          for view:UnitCell in self.subviews as [UnitCell]{
  27:              if view.tag==j{
  28:                  return view
  29:              }
  30:          }
  31:          println("Impossible!!!!!!!!!")
  32:          return UnitCell()
  33:      }
  34:  }

同理,在Storyboard中复制9份UnitCell,如图设置父类

Screen Shot 2015-03-05 at 5.32.43 PM

每个UnitCell要设置 Tag,方便编程时获取,我们这里把第一个tag设置为0……第9个设置为8

到这里Xcode就开始有点卡了,建议建一个临时的xib文件进行设置,确定设置好了再拖到storyboard中。

(2)我们称能同时显示9个S9Unit格子的单元格为SNNUnit,

   1:  //
   2:  //  S99Unit.swift
   3:  //  sudoku-v02
   4:  //
   5:  //  Created by 张泽阳 on 2/14/15.
   6:  //  Copyright (c) 2015 张泽阳. All rights reserved.
   7:  //
   8:   
   9:  import Foundation
  10:  import UIKit
  11:  /**
  12:      包含9个九宫格(S9Unit)的九宫格
  13:  */
  14:  class SNNUnit: UIView {
  15:      // MARK: - 初始化单元格
  16:      func initAllCells(){
  17:  //        self.layer.borderWidth=3
  18:          self.hidden = false 
  19:          for s9view:S9Unit in self.subviews as [S9Unit]{
  20:  //            println("S99's subviews:\(s9view.description)\n\(s9view.tag)")
  21:              s9view.initAllCells()
  22:          }
  23:          self.layer.borderWidth=4
  24:      }
  25:      // MARK: - 取得指定tag单元格 i,j:i大格j小格
  26:      func getTheUnitCell(i:Int,j:Int) -> UnitCell{
  27:          for s9view:S9Unit in self.subviews as [S9Unit]{
  28:              //            println("S99's subviews:\(s9view.description)\n\(s9view.tag)")
  29:              if s9view.tag==(90+i){
  30:                  
  31:                   return s9view.getTheUnit(j)
  32:                  
  33:                  
  34:              }
  35:          }
  36:          
  37:          println("Impossible!!!\(i) \(j)")
  38:          return UnitCell()
  39:      }
  40:      
  41:  }

界面设置如图

Screen Shot 2015-03-05 at 5.33.21 PM

长宽540*540

 

 

2)数字选择视图(NumberPickerView)的创建

(1)数字选择视图如图右下角,

Screen Shot 2015-02-24 at 9.49.16 PM

功能是在点击左侧的UnitCell后,点击右边的NumberPickerView中的一个数字,就可以把数字填进左侧所选空格中。

(2)首先建立196*196的View,取名NumberPickerView,内部存有9个数字Button,每个数字长宽60*60。

每个Button也设置一个TouchUpInside事件,我们取名为onTouchNumberPicker,并加上outlet方便调取。

Screen Shot 2015-03-06 at 9.59.33 AM

3)设置标记视图(MarkerPickerView)

如图

Screen Shot 2015-03-06 at 10.21.56 AM

196*196,每个格子是一个UnitCell,设置颜色如图,这里的UnitCell注意要重新设置TouchUpInside事件,我们取名为onTouchMarkerUnitCell,并加上outlet方便调取。

 

4)设置取消按钮

Screen Shot 2015-03-06 at 10.25.11 AM 添加如图Button,背景设置为网上找的一张图片。设置TouchUpInside事件名称为onTouchCancelButton,并加上outlet方便调取。

5)设置看答案Button和返回Button

右上角加上这两个Button,设置TouchUpInside名称分别为 onAnswerAsked 和 onExitTouched。

3)构建游戏界面逻辑代码,备注什么的我尽量写详细了。判断游戏是否有错的方法为checkTotalAgain

注意由上一个选关界面转到游戏界面后,推出时应调用的代码为onExitTouched方法中的dismissViewControllerAnimated

由segue向下一个看答案界面传递数据时用prepareForSegue方法中的代码。

   1:  //
   2:  //  ViewController.swift
   3:  //  sudoku-v02
   4:  //
   5:  //  Created by 张泽阳 on 2/13/15.
   6:  //  Copyright (c) 2015 张泽阳. All rights reserved.
   7:  //
   8:   
   9:  import UIKit
  10:  class ViewController: UIViewController,remainProtocol,UIAlertViewDelegate {
  11:      // MARK: - 变量
  12:  //    var pickedNumberArray =  Array <Array <Int>> ()
  13:  //    var pickedMarkerArray =  Array <Array <String>> ()
  14:      var currentPickingUnitCell:UnitCell? = nil
  15:      var prePass = pass(mytitle: "", isPassed: false, prob: "", solves: [""])
  16:  //    var cellTemp:UITableViewCell?
  17:  //    var preInitials:Array <Array <Int>> = Array <Array <Int>>()
  18:      var remainCells = 81
  19:      var isSucc = false
  20:      var notErr = true
  21:      var remainCellsCounter:RemainCellsCounter? = nil
  22:  //    var unavailableNums = Array <Bool> ()//标记每次不可选的数字 如果选的话就错了
  23:  //    var errPosition = Array<(Int,Int)>()
  24:      // MARK: - 实现代理方法,处理成功时的方法
  25:      func remainZero() {
  26:          if notErr {succ()}
  27:      }
  28:      func succ(){
  29:          savePassedByMD5(prePass.problem)
  30:          isSucc = true
  31:          UIAlertView(title: "祝贺你", message: "成功解决这个数独游戏题!", delegate: self, cancelButtonTitle: "确定").show()
  32:      }
  33:      // MARK: - 保存通过关卡的状态
  34:      func savePassedByMD5(pro:String){
  35:          var md5 = (pro as NSString).md5()
  36:          //        NSUserDefaults.standardUserDefaults().
  37:          //        println(NSUserDefaults.standardUserDefaults().boolForKey(md5))
  38:          NSUserDefaults.standardUserDefaults().setBool(true, forKey: md5)
  39:          NSUserDefaults.standardUserDefaults().synchronize()
  40:          //            boolForKey(md5)
  41:          
  42:      }
  43:        // MARK: - 标记视图的相关操作
  44:      @IBOutlet weak var myMarkers: UIView!
  45:     
  46:      @IBAction func onTouchMarkerUnitCell(sender: UnitCell) {
  47:         
  48:  //            println("onTouchMarkerPicker")
  49:  //            println("\(sender.tag) curr=\(currentPickingUnitCell?.tag)")
  50:              currentPickingUnitCell?.addNumberMark(sender.tag)
  51:              disableMarkerPickerAndNumberPicker()
  52:              currentPickingUnitCell = nil
  53:          
  54:      }
  55:          func enableCancelButton(){
  56:          cancelButton.enabled = true
  57:      }
  58:        // MARK: - 数字选择视图的相关操作
  59:      @IBOutlet weak var myNumberPickerView: NumberPickerView!
  60:      @IBAction func onTouchNumberPicker(sender: UIButton) {
  61:  //        println("touched number picker")
  62:  //        println(" tag = \(sender.tag)")
  63:          if currentPickingUnitCell?.titleLabel?.text == "" {
  64:  //             --remainCells
  65:              currentPickingUnitCell?.setCertainNumberBig(sender.tag)
  66:              disableMarkerPickerAndNumberPicker()
  67:              checkTotalAgain()
  68:              remainCellsCounter?.dec()
  69:          }else{
  70:              currentPickingUnitCell?.setCertainNumberBig(sender.tag)
  71:              disableMarkerPickerAndNumberPicker()
  72:              checkTotalAgain()
  73:              remainCellsCounter?.notChanged()
  74:          }
  75:  //        checkNumberAvailable(sender.tag)
  76:          
  77:         
  78:  //       println("remainCells\(remainCellsCounter?.count)")
  79:      }
  80:      // MARK: - 使标记视图和数字选择视图可以点击
  81:      func enableMarkerPickerAndNumberPicker(){
  82:  //        cancelButton.enabled = true
  83:          cancelButton.hidden = false
  84:          currentPickingUnitCell?.backgroundColor = UIColor.blueColor()
  85:          for view in myMarkers.subviews as [UnitCell] {
  86:              view.enabled = true
  87:          }
  88:          myMarkers.hidden = false
  89:          for view in myNumberPickerView.subviews as [UIButton] {
  90:              view.enabled = true
  91:          }
  92:          myNumberPickerView.hidden = false
  93:          var (i,j) = getIJwithCertainUnitCell(currentPickingUnitCell!)
  94:  //        println("i\(i) j\(j)")
  95:  //        disableCorrespondingButtonWithIJ(i,jj: j)
  96:          //MARK TODO 参数标记需要优化
  97:  //        makeUnavailableButtonWithIJ(i,jj: j)
  98:      }
  99:      // MARK: - 使标记视图和数字选择视图不可以点击
 100:      func disableMarkerPickerAndNumberPicker(){
 101:          cancelButton.enabled = false
 102:          cancelButton.hidden = true
 103:          currentPickingUnitCell?.backgroundColor = UIColor.whiteColor()
 104:          for view in myMarkers.subviews as [UnitCell] {
 105:              view.enabled = false
 106:          }
 107:          myMarkers.hidden = true
 108:          for view in myNumberPickerView.subviews as [UIButton] {
 109:              view.enabled = false
 110:          }
 111:          myNumberPickerView.hidden = true
 112:          currentPickingUnitCell = nil
 113:      }
 114:     
 115:      func checkTotalAgain(){
 116:          for i in 0...8 {
 117:              var t = 0
 118:              for j in 0...8 {
 119:  //                println("i,j\(i),\(j)  \(getTheUnitCell(i, j: j).titleLabel?.text)")
 120:                  if getTheUnitCell(i, j: j).titleLabel!.text!.isEmpty {
 121:                      continue
 122:                  }
 123:                  var k = getTheUnitCell(i, j: j).titleLabel!.text!.toInt()!;
 124:                  var m = t&(1<<k)
 125:                  if m != 0 {
 126:                      notErr = false
 127:                      return
 128:                  }else{
 129:                      t+=1<<k
 130:                  }
 131:              }
 132:          }
 133:          for i in 0...8 {
 134:              var t = 0
 135:              for j in 0...8 {
 136:                  if getTheUnitCell(j, j: i).titleLabel!.text!.isEmpty {
 137:                      continue
 138:                  }
 139:                  var k = getTheUnitCell(j, j: i).titleLabel!.text!.toInt()!;
 140:                  var m = t&(1<<k)
 141:                  if m != 0 {
 142:                      notErr = false
 143:                      return
 144:                  }else{
 145:                      t+=1<<k
 146:                  }
 147:              }
 148:          }
 149:          for a in 0...8 {
 150:              var t = 0
 151:              for b in 0...8 {
 152:                  var ii = (a/3)*3+b/3
 153:                  var jj = (a%3)*3 + b%3
 154:                  if getTheUnitCell(ii, j: jj).titleLabel!.text!.isEmpty {
 155:                      continue
 156:                  }
 157:                  var k = getTheUnitCell(ii, j: jj).titleLabel!.text!.toInt()!;
 158:                  var m = t&(1<<k)
 159:                  if m != 0 {
 160:                      notErr = false
 161:                      return
 162:                  }else{
 163:                      t+=1<<k
 164:                  }
 165:              }
 166:          }
 167:          notErr = true
 168:          
 169:      }
 170:      // MARK: - (废弃)给定i,j坐标搜索无效的数字并禁止点按
 171:      func disableCorrespondingButtonWithIJ(ii:Int,jj:Int){
 172:          for i in 0...8 {
 173:              if i == ii {continue}
 174:              var unit = getTheUnitCell(i, j: jj);
 175:              if unit.titleLabel?.text != "" {
 176:  //                println(unit.titleLabel?.text)
 177:                  clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 178:              }
 179:          }
 180:          for j in 0...8 {
 181:              if j == jj {continue}
 182:              var unit = getTheUnitCell(ii, j: j);
 183:              if unit.titleLabel?.text != "" {
 184:                  clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 185:              }
 186:          }
 187:          var si = ii/3*3
 188:          var sj = jj/3*3
 189:          for i in si...si+2 {
 190:              for j in sj...sj+2 {
 191:                  if (i == ii)&&(j == jj) {continue}
 192:                  var unit = getTheUnitCell(i, j: j);
 193:                  if unit.titleLabel?.text != "" {
 194:                      clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 195:                  }
 196:              }
 197:          }
 198:          
 199:      }
 200:      // MARK: - (废弃)使标记视图和数字选择视图的某些数字不可选择
 201:      func clearMarkerPickerAndNumberPickerWithNumber(i:Int){
 202:  //        println("clear \(i)!!!")
 203:          for view in myMarkers.subviews as [UnitCell] {
 204:  //            println(view.tag)
 205:              if view.tag == i {
 206:                  view.enabled = false
 207:  //                println("view enable \(view.enabled)")
 208:                  break
 209:              }
 210:          }
 211:          for view in myNumberPickerView.subviews as [UIButton] {
 212:  //            println(view.tag)
 213:              if view.tag == i {
 214:                  view.enabled = false
 215:  //                println("view enable \(view.enabled)")
 216:                  break
 217:              }
 218:          }
 219:      }
 220:      // MARK: - 视图初始化相关操作
 221:      @IBOutlet weak var s99unit: SNNUnit!
 222:      @IBOutlet weak var cancelButton: UIButton!
 223:      override func viewDidLoad() {
 224:          super.viewDidLoad()
 225:  //       println(prePass.problem)
 226:          initAllMyCells()
 227:  //        initUnavailableNums()
 228:           remainCellsCounter = RemainCellsCounter(delegate1: self)
 229:          //TODO 
 230:  //        initArrays()
 231:  //        initArrays
 232:          initArrays(prePass.problem)
 233:  //        remainCellsCounter?.count = remainCells
 234:         
 235:   
 236:      }
 237:      // MARK: - 设置点击取消按钮时的操作
 238:      @IBAction func onTouchCancelButton(sender: UIButton) {
 239:          currentPickingUnitCell?.initCells()
 240:  //        ++remainCells
 241:          remainCellsCounter?.inc()
 242:          disableMarkerPickerAndNumberPicker()
 243:          checkTotalAgain()
 244:      }
 245:      
 246:      
 247:      
 248:      // MARK: - 数组初始化
 249:   
 250:      func initArrays(withCertainInitials:Array <Array <Int>>){
 251:          for i in 0...8 {
 252:              for j in 0...8{
 253:                  if withCertainInitials[i][j]==0 {
 254:                      continue
 255:                  }else{
 256:                      lockUnitCell(i,j: j)
 257:                      setUnitCellWithNumber(i,j: j,v: withCertainInitials[i][j])
 258:                  }
 259:              }
 260:          }
 261:      }
 262:      func initArrays(withString:NSString){
 263:          for i in 0...8 {
 264:              for j in 0...8{
 265:                  var t:String = withString.substringWithRange(NSMakeRange(i*9+j, 1))
 266:                  var cnum = t.toInt()
 267:                  if cnum == 0 {
 268:                      continue
 269:                  }else{
 270:                      lockUnitCell(i,j: j)
 271:                      setUnitCellWithNumber(i,j: j,v: cnum!)
 272:                      
 273:                  }
 274:              }
 275:          }
 276:      }
 277:      
 278:      
 279:      // MARK: - 单元格相关操作
 280:      @IBAction func onUnitCellTouch(sender: UnitCell) {
 281:          //        println("touched remainCells\(remainCells)")
 282:          //        println(" tag =\(sender.superview?.tag) \(sender.tag)")
 283:          if (sender == currentPickingUnitCell) {
 284:              //            println("same!")
 285:              currentPickingUnitCell = nil
 286:              sender.backgroundColor = UIColor.whiteColor()
 287:              disableMarkerPickerAndNumberPicker()
 288:              return
 289:          }
 290:          if currentPickingUnitCell != nil {
 291:              currentPickingUnitCell?.backgroundColor = UIColor.whiteColor()
 292:              currentPickingUnitCell = nil
 293:          }
 294:          
 295:          currentPickingUnitCell = sender
 296:  //        clearUnavailableNumber()//每次按下不同的单元格无效数字需清空
 297:          enableMarkerPickerAndNumberPicker()
 298:          if currentPickingUnitCell?.titleLabel?.text != "" {
 299:              enableCancelButton()
 300:          }
 301:          
 302:      }
 303:   
 304:      //UnitCell Ops
 305:      func getTheUnitCell(i:Int, j:Int)->UnitCell{
 306:          var a = (i/3)*3+j/3
 307:          var b = (i%3)*3+j%3
 308:  //        println("(\(i),\(j))-> \(a), \(b)")
 309:         return s99unit.getTheUnitCell(
 310:         a
 311:          ,j: b
 312:          )
 313:          
 314:      }
 315:      func getIJwithCertainUnitCell(unit:UnitCell)->(i:Int,j:Int){
 316:          var b = unit.tag
 317:          var a = unit.superview!.tag % 10
 318:  //        println("touch unit a\(a) b\(b)")
 319:          var ii = (a/3)*3+b/3
 320:          var jj = (a%3)*3 + b%3
 321:          return (ii,jj)
 322:          
 323:      }
 324:      func setUnitCellWithNumber(i:Int,j:Int,v:Int){
 325:          var unit = getTheUnitCell(i,j: j)
 326:          unit.setCertainNumberBig(v)
 327:  //        --remainCells
 328:          remainCellsCounter?.dec()
 329:      }
 330:      func lockUnitCell(i:Int,j:Int){
 331:          var unit = getTheUnitCell(i,j: j)
 332:          unit.lockUnit()
 333:      }
 334:      func highlightUnitCell(cell:UnitCell,withColor:UIColor){
 335:          cell.backgroundColor = withColor
 336:      }
 337:      func initAllMyCells(){
 338:          s99unit.initAllCells()
 339:          for markerView in myMarkers.subviews as [UnitCell]{
 340:              markerView.layer.borderWidth=1
 341:  //            markerView.setBackgroundImage(UIImage(named: "banBack"), forState: UIControlState.Disabled)
 342:          }
 343:          for numPickerView in myNumberPickerView.subviews as [UIButton]{
 344:              numPickerView.layer.borderWidth = 1
 345:  //            numPickerView.setBackgroundImage(UIImage(named: "banBack"), forState: UIControlState.Disabled)
 346:          }
 347:          
 348:          disableMarkerPickerAndNumberPicker()
 349:         
 350:      }
 351:      // MARK: - 退出按钮操作
 352:      @IBAction func onExitTouched(sender: UIButton) {
 353:  //        println("t")
 354:  //        self.navigationController?.popToRootViewControllerAnimated(true)
 355:  //        self.dismissViewControllerAnimated(true, completion:^{
 356:  //            
 357:  //            })
 358:          self.dismissViewControllerAnimated(true, completion: { () -> Void in
 359:              if self.isSucc {
 360:              NSNotificationCenter.defaultCenter().postNotificationName("reload", object: self)
 361:              }
 362:          })
 363:      }
 364:      // MARK: - 看答案按钮相关操作
 365:      @IBAction func onAnswerAsked(sender: AnyObject) {
 366:      
 367:  //        var alert:UIAlertView!
 368:          
 369:          if prePass.solves.count>1 {
 370:               UIAlertView(title: "看答案", message: "确认看答案?", delegate: self, cancelButtonTitle: "取消",otherButtonTitles:"答案一","答案二").show()
 371:          }else{
 372:               UIAlertView(title: "看答案", message: "确认看答案?", delegate: self, cancelButtonTitle: "取消",otherButtonTitles:"答案一").show()
 373:   
 374:          }
 375:          
 376:         
 377:      }
 378:      var currentAns = 0
 379:      // MARK:  alertview代理实现
 380:      func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
 381:  //        println("touched \(alertView.title) \(buttonIndex) \(alertView.buttonTitleAtIndex(buttonIndex))")
 382:          if alertView.title == "看答案" {
 383:              
 384:  //          
 385:              if alertView.buttonTitleAtIndex(buttonIndex) != "取消" {
 386:                  currentAns = buttonIndex - 1
 387:                  self.performSegueWithIdentifier("ans", sender: nil)
 388:   
 389:              }
 390:              
 391:          }
 392:      }
 393:   
 394:      
 395:      // MARK:  设置看答案时传递的参数
 396:      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
 397:          if segue.identifier == "ans" {
 398:              var detailViewController:ShowAnswerController! = segue.destinationViewController as ShowAnswerController
 399:              //            var index = self.tableView.indexPathForSelectedRow()
 400:              detailViewController.ans = prePass.solves[currentAns]
 401:              //            detailViewController.cellTemp = (sender as UITableViewCell)
 402:   
 403:          }
 404:      }
 405:      override func didReceiveMemoryWarning() {
 406:          super.didReceiveMemoryWarning()
 407:          // Dispose of any resources that can be recreated.
 408:      }
 409:      // MARK: - 不用的代码以作备用
 410:      func junks(){
 411:          //    override func targetViewControllerForAction(action: Selector, sender: AnyObject?) -> UIViewController? {
 412:          //        return ShowAnswerController()
 413:          //    }
 414:          //Initials
 415:          //    func initArrays(){
 416:          //        for i in 0...8
 417:          //        {
 418:          //            pickedNumberArray.append(Array(count:9,repeatedValue:0))
 419:          //        }
 420:          //        for i in 0...8
 421:          //        {
 422:          //            pickedMarkerArray.append(Array(count:9,repeatedValue:""))
 423:          //        }
 424:          //    }
 425:          /*
 426:          var boxLength = min(Int(self.view.frame.width)/5,Int(self.view.frame.height)/5)
 427:          println(boxLength)
 428:          var i=0,j=0
 429:          var marginY = (Int(self.view.frame.width) - boxLength*3)/4
 430:          var marginX = (Int(self.view.frame.height) - boxLength*3)/4
 431:          //        for i in 0...8 {
 432:          //            var sudokuView:UIView = UIView()
 433:          //            sudokuView.backgroundColor = UIColor.redColor()
 434:          //            sudokuView.frame = CGRectMake(
 435:          //                margin*CGFloat(i-1)+CGFloat(boxLength*(i%3))
 436:          //                ,margin*CGFloat(i-1)+CGFloat( boxLength*(i/3))
 437:          //                , CGFloat(boxLength), CGFloat(boxLength))
 438:          ////            sudokuView.
 439:          //            self.view.addSubview(sudokuView)
 440:          //            }
 441:  
 442:          for i in 0..<3 {
 443:          for j in 0..<3 {
 444:          var sudokuView:UIView = UIView()
 445:          sudokuView.backgroundColor = UIColor.whiteColor()
 446:          sudokuView.frame = CGRectMake(
 447:          CGFloat(boxLength*(i)+(i+1)*marginY)
 448:          ,CGFloat( boxLength*(j)+(j+1)*marginX)
 449:          ,CGFloat(boxLength), CGFloat(boxLength)
 450:          )
 451:          sudokuView.layer.borderWidth=2
 452:          sudokuView.layer.borderColor=UIColor.blackColor().CGColor
 453:          self.view.addSubview(sudokuView)
 454:          }
 455:          }*/
 456:          
 457:          //        println(((self.view.subviews.first) as UIView).description)
 458:          //         println( ((self.view.subviews.first) as S99Unit).isKindOfClass(S99Unit) )
 459:          //        (self.view.subviews.first as S99Unit).initAllCells()
 460:          //        for view in self.view.subviews{
 461:          //            println(view.description)
 462:          //            if view.tag==1000 {
 463:          ////                    view.name
 464:          //                println(view.name)
 465:          //                (view as S99Unit).initAllCells()
 466:          //            }
 467:          //        }
 468:          //        println(s99unit .isKindOfClass(SNNUnit))
 469:          //        println(s99unit .isKindOfClass(UIView))
 470:          //        println(s99unit)
 471:          
 472:          //        myMarkers.layer.borderWidth=2
 473:          ////        myMarkerPickerView.backgroundColor = UIColor.brownColor()
 474:          //        for markerView in myMarkerPickerView.subviews as [UnitCell]{
 475:          //            println("got 1 unit\(markerView.description)")
 476:          //            myMarkerPickerView.bringSubviewToFront(markerView)
 477:          //            markerView.hidden = false
 478:          //            for eachUnit in markerView.subviews as [UILabel]{
 479:          //                eachUnit.hidden = false
 480:          //                eachUnit.layer.borderWidth=2
 481:          //
 482:          //            }
 483:          //            markerView.backgroundColor = UIColor.blackColor()
 484:          //            markerView.layer.borderWidth=2
 485:          ////            markerView.frame = CGRectMake(CGFloat(715), CGFloat(20), CGFloat(60), CGFloat(60))
 486:          //        }
 487:          /* // MA_RK: -给定i,j坐标搜索无效的数字并标记
 488:          func makeUnavailableButtonWithIJ(ii:Int, jj:Int){
 489:          for i in 0...8 {
 490:          if i == ii {continue}
 491:          var unit = getTheUnitCell(i, j: jj);
 492:          if unit.titleLabel?.text != "" {
 493:          //                println(unit.titleLabel?.text)
 494:          markNumUnavailable(unit.titleLabel!.text!.toInt()!)
 495:          //                clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 496:          }
 497:          }
 498:          for j in 0...8 {
 499:          if j == jj {continue}
 500:          var unit = getTheUnitCell(ii, j: j);
 501:          if unit.titleLabel?.text != "" {
 502:          //                clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 503:          markNumUnavailable(unit.titleLabel!.text!.toInt()!)
 504:          }
 505:          }
 506:          var si = ii/3*3
 507:          var sj = jj/3*3
 508:          for i in si...si+2 {
 509:          for j in sj...sj+2 {
 510:          if (i == ii)&&(j == jj) {continue}
 511:          var unit = getTheUnitCell(i, j: j);
 512:          if unit.titleLabel?.text != "" {
 513:          //                    clearMarkerPickerAndNumberPickerWithNumber(unit.titleLabel!.text!.toInt()!)
 514:          markNumUnavailable(unit.titleLabel!.text!.toInt()!)
 515:          }
 516:          }
 517:          }
 518:  
 519:          }*/
 520:          /*  // MA_RK: - 标记无效数字
 521:          func markNumUnavailable(n:Int){
 522:          //        unavailableNum ^= n
 523:          unavailableNums[n] = true
 524:          }
 525:          // MA_RK:  检查所选数字是否有效
 526:          func checkNumberAvailable(n:Int){
 527:          //        return (unavailableNum&n != 0)
 528:          if unavailableNums[n] == true {
 529:          notErr = false
 530:          }
 531:          }
 532:          // MAR_K:  清空无效数字标记
 533:          func clearUnavailableNumber(){
 534:          var i = 0
 535:          for i=0;i<unavailableNums.count;++i {
 536:          unavailableNums[i] = false
 537:          }
 538:          }
 539:          // MA_RK:  初始化无效数字
 540:          func initUnavailableNums(){
 541:          var i = 0
 542:          for i=0;i<10;++i{
 543:          unavailableNums.append(false)
 544:          }
 545:          }
 546:          */
 547:      }
 548:  }
 549:   
原文地址:https://www.cnblogs.com/zeyang/p/4317503.html