SpriteKit中的UIPickerView

时间:2016-02-12 12:31:59

标签: swift sprite-kit uitextfield uipickerview

我将UIPickerView添加到SpriteKit场景中。代码如下所示:

import UIKit
import SpriteKit

class MyScene: SKScene, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
    let myField = UITextField(frame: CGRectMake(UIScreen.mainScreen().bounds.size.width * 0.4, UIScreen.mainScreen().bounds.size.height * 0.35, 200, 30))
    let myPickOptions = ["One", "Two", "Three"]

    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        let myPickerView  : UIPickerView = UIPickerView()
        myField.inputView = genderPickerView
        myField.font = UIFont.systemFontOfSize(15)
        myField.borderStyle = UITextBorderStyle.RoundedRect
        myPickerView.dataSource = self
        myField.delegate = self
        self.view!.addSubview(myField)
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
        return 1
    }

    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
        return myPickOptions.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return myPickOptions[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        myField.text = "\(myPickOptions[row])"
    }

当我在textField内部点击但没有数据(选择器中只有一些问号)时,屏幕上会出现选择器。

知道什么是错的吗?

其他编辑: 当我查看两个数据源方法时,我看到正确返回的行数,尽管它被返回了28次。也许这很正常,但看起来很奇怪。当我查看委托方法时,看起来第一个没有被调用(没有返回行数据)。

并解决了它...... 我需要添加第二个代表:

myPickerView.delegate = self

1 个答案:

答案 0 :(得分:0)

I missed the second delegate.. all good when it's added