2016-01-05 92 views
0

您好我一直在關注上的Xcode 7.2一些教程,但是這對於那些迅速1.2 我認爲它SWIFT 2.1 他使用的UITextField與spriteKit 但此行會導致錯誤 我已經用Google搜索,但找不到任何幫助 所以認爲這是幫助歡呼添加到的UITextField精靈套件

self.view!.addSubview(TextInput!) 

整個代碼是

進口SpriteKit

的地方
class GameScene: SKScene 
{ var TextInput:UITextField? 

override func didMoveToView(view: SKView) 
{ 

    let myLabel = SKLabelNode(fontNamed:"Chalkduster") 
    myLabel.text = "" 
    myLabel.fontSize = 15 
    myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame)) 

    self.addChild(myLabel) 

    TextInput?.frame = CGRect(x: 200, y: 300, width: 100, height: 40) 

    self.view!.addSubview(TextInput!) 

    TextInput?.backgroundColor = UIColor.whiteColor() 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    /* Called when a touch begins */ 

    for touch in touches 
    { 
    let location = touch.locationInNode(self) 

    } 
} 

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
} } 

回答

0

您似乎並未在任何地方初始化您的UITextField。 嘗試加入這行來didMoveToView您設置的TextInput的幀之前:

self.TextInput = UITextField() 

只是要注意,在斯威夫特變量名應在駝峯(thisIsCamelCase)編寫的,所以它可能是最好的變量重命名爲textInput。我現在實際上無法在Apple文檔中找到它,但是這個約定用於Apple所有示例代碼中,並且由各種其他來源聲明,例如,

https://github.com/raywenderlich/swift-style-guide

http://andybargh.com/variables-and-constants-in-swift/#Rules_for_Naming_Variables

+0

不是非常有幫助的,是嗎?我嘗試了同樣的事情。我的'TextInput'被初始化。但沒有添加視圖,或者我無法在iPhone 5s上看到它。 – Boggartfly