要点是设置好Tag就好,通过代码找到并初始化即可。
1: //
2: // ShowAnswerController.swift
3: // sudoku-v02
4: //
5: // Created by 张泽阳 on 2/22/15.
6: // Copyright (c) 2015 张泽阳. All rights reserved.
7: //
8:
9: import Foundation
10: import UIKit
11: @IBDesignable
12: class ShowAnswerController: UIViewController {
13: // MARK: - 变量
14: @IBOutlet weak var av: UIView!
15: var ans:String?
16: var mans:NSString?
17: override func viewDidLoad() {
18: // println(ans)
19: // for i in 1...9 {
20: // println(av.viewWithTag(i))
21: // }
22: mans = ans
23: initCells()
24: }
25: // MARK: - 初始化单元格
26: func initCells()
27: {
28: av.layer.borderWidth = 4
29: for bc in av.subviews {
30: var a = bc.tag % 10
31: bc.layer.borderWidth = 2
32: for sbc in bc.subviews {
33: var b = sbc.tag
34: // sbc.layer.borderWidth = 1
35: var ii = ((a-1)/3)*3+(b-1)/3
36: var jj = ((a-1)%3)*3 + (b-1)%3
37: println("a\(a)b\(b)i\(ii)j\(jj)")
38: var sub = mans?.substringWithRange(NSMakeRange(((ii)*9)+jj,1))
39: (sbc as UIButton).setTitle(sub, forState: UIControlState.Normal)
40: // println("sbc \(sbc) a\(a)b\(b)sub\(sub)")
41: }
42: }
43: }
44: // MARK: - 后退
45: @IBAction func back(sender: AnyObject) {
46: self.dismissViewControllerAnimated(true, completion: nil)
47: }
48: }
请发表评论