2015-07-09 39 views
0

我有一個名爲LifeCounter的UIView子類,我想爲每個玩家創建一個子類。我能夠爲Main.storyboard添加兩個視圖,通過屬性面板將他們的類設置爲LifeCounter,並通過這種方式創建多個實例,連接到視圖控制器,更改屬性等。UIView子類可以創建其他自定義視圖實例嗎?

我現在想的是創建一個更大的視圖GameHeader,它可以容納LifeCounters和一些其他補充信息,比如時間,遊戲重置按鈕等。GameHeader是一個UIView子類,但是我無法在模擬器中繪製LifeCounter視圖,而我不知道爲什麼。

GameHeader目前是一個拖入故事板的視圖,並且它是具有Attributes面板的類。

GameHeader.swift

import UIKit 

class GameHeader: UIView { 

// MARK: Properties 

// The Frame 
let topFrame = CGRect(x: 0, y: 0, width: 320, height: 200) 

// MARK: Initlization 
required init() { 
    super.init(frame: topFrame) 

    // Adds the player one counter 
    let playerOneCounter = LifeCounter() 
    addSubview(playerOneCounter) 

} 

required init(coder aDecoder: NSCoder) { 
    // Calls the super class (UIView) initializer 
    super.init(coder: aDecoder) 
} 

} 

LifeCounter.swift

import UIKit 

class LifeCounter: UIView { 

// MARK: Propterties 

// Starting life total 
var lifeTotal = 20 { 
    didSet { 
     // Updates the layout whenever the lifeTotal is updated 
     setNeedsLayout() 
    } 
} 

// Creates the UI Labels 
// All created views need a defined frame for where they sit 
// Is this neccesary outside of the init? Is there a better way? 
var counter = UILabel(frame: CGRect(x: 0, y: 20, width: 100, height: 90)) 
var playerName = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40)) 
var winner = "" 



// MARK: Initlization 

// First init. LifeCounter takes a frame parameter, the adds the labels etc. 
init() { 
    super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 100)) 
    self.addLifeCounter() 
} 

required init(coder aDecoder: NSCoder) { 
    // Calls the super class (UIView) initializer 
    super.init(coder: aDecoder) 
} 

func addLifeCounter() { 
    print("addLifeCounter is running") 
    // Styles life counter label 
    counter.textColor = UIColor.blackColor() 
    counter.textAlignment = .Center 
    counter.font = UIFont.boldSystemFontOfSize(72) 
    counter.text = String(lifeTotal) 

    // Styles playerName label 
    playerName.text = "Player Name" 
    playerName.textAlignment = .Center 


    // Button 
    let minusButton = UIButton(frame: CGRect(x: 5, y: 110, width: 40, height: 40)) 
    let plusButton = UIButton(frame: CGRect(x: 55, y: 110, width: 40, height: 40)) 

    minusButton.backgroundColor = UIColor.redColor() 
    plusButton.backgroundColor = UIColor.blueColor() 

    // Button action 
    minusButton.addTarget(self, action: "minusLife:", forControlEvents: .TouchDown) 
    plusButton.addTarget(self, action: "plusLife:", forControlEvents: .TouchDown) 

    addSubview(playerName) 
    addSubview(counter) 
    addSubview(minusButton) 
    addSubview(plusButton) 
} 


// MARK: Button actions 
func minusLife(minusButton: UIButton) { 
    lifeTotal -= 1 
    counter.text = String(lifeTotal) 
} 

func plusLife(plusButton: UIButton) { 
    lifeTotal += 1 
    counter.text = String(lifeTotal) 
} 

} 

回答

1

init(coder:)是當從一個情節串連圖板或筆尖創建的視圖被稱爲初始化。如果您移動在那裏創建並添加LifeCounter實例的代碼,它應該可以工作。

使其更加可重用的一個好方法是創建一個從兩個初始化程序中調用的設置方法,以便它可以運行,無論它來自故事板/筆尖還是以編程方式實例化。

class GameHeader: UIView { 
    let topFrame = CGRect(x: 0, y: 0, width: 320, height: 200) 

    required init() { 
     super.init(frame: topFrame) 
     self.setupSubviews() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.setupSubviews() 
    } 

    func setupSubviews() { 
     let playerOneCounter = LifeCounter() 
     addSubview(playerOneCounter) 
    } 

} 
+0

非常感謝! –

0

我發現錯誤是將LifeController繪圖代碼放入init(frame :)塊而不是使用Coder塊。當通過Interface Builder添加視圖時,它使用init(代碼:)而不是init(框架:)

+0

打我吧! :) –

+0

我可能已經找出了錯誤,但感謝提高效率! –

相關問題