2014-12-18 51 views
0

我正嘗試重置按鈕單擊上的UIPickerView。搜索的StackOverflow後,我發現:如何將Objective C重寫爲RubyMotion

[picker reloadAllComponents]; 
[picker selectRow:0 inComponent:0 animated:YES]; 

我重寫它在紅寶石這樣的:

def reset_picker 
    @picker.reloadAllComponents 
    @picker.selectRow(0, inComponent: 0, animated: true) 
    end 

上面僅重置第一分量;我如何重置所有三個組件?我知道在reset_picker方法中有些事情要做inComponent:0

下面是UIPickerView方法調用:

def numberOfComponentsInPickerView(pickerView) 
    component_options.count 
    end 

    def pickerView(pickerView, numberOfRowsInComponent: component) 
    component_options[component].count 
    end 

    def pickerView(pickerView, titleForRow: row, forComponent: component) 
    component_options[component][row] 
    end 

回答

0

你是正確的 - inComponent: 0涉及組件的索引。

考慮到這一點,重置所有三個組件,你只需要:

def reset_picker 
    @picker.reloadAllComponents 
    @picker.selectRow(0, inComponent: 0, animated: true) 
    @picker.selectRow(0, inComponent: 1, animated: true) 
    @picker.selectRow(0, inComponent: 2, animated: true) 
end 
+0

感謝您的幫助,該訣竅! – Hyetigran 2014-12-19 00:46:38

+0

沒問題,很高興我能幫到你。次要的事情,但你不應該需要對'reloadAllComponents'的初始調用,我只是保持它適合你的代碼。 – InsertWittyName 2014-12-19 00:48:06

+0

你說得對,沒有'reloadAllComponents',它工作正常。我可以把它放在'self.numberOfComponentsInPickerView.times do | x |'塊嗎? – Hyetigran 2014-12-19 17:00:35