2014-06-09 8 views
7
के लिए स्विफ्ट में प्रतिनिधि के तरीके

बस स्विफ्ट में शुरू किया था और हो रही परेशानी एक UIPickerView के लिए प्रतिनिधि तरीकों बुला होनेUIPickerView

अब तक मैं बहुत की तरह मेरी कक्षा के लिए UIPickerViewDelegate जोड़ लिया है:

class ExampleClass: UIViewController, UIPickerViewDelegate 

मेरे पास है भी मेरी UIPickerView बनाई गई है और इसके लिए प्रतिनिधि सेट:

@IBOutlet var year: UIPickerView 
year.delegate = self 

अब मैं सिर्फ मुसीबत स्विफ्ट कोड में निम्न मोड़ कर रहा हूँ:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 

किसी भी मदद की सराहना की जाएगी

उत्तर

0

प्रतिनिधि कि विधि फोन करने के लिए ज़िम्मेदार नहीं है। इसके बजाय इसे यूआईपीकर व्यू के डेटा स्रोत द्वारा बुलाया जाता है। इन दोनों कार्यों UIPickerView डेटा स्रोत से बुलाया है कि आप को लागू करने की जरूरत है:

// returns the number of 'columns' to display. 
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int 

// returns the # of rows in each component.. 
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int 

इन कार्यों सुनिश्चित करने के लिए कहा जाता हो जाएगा तो आपकी वर्ग भी डेटा स्रोत प्रोटोकॉल को लागू करना चाहिए:

class ExampleClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

और अपने पिकर के डेटा स्रोत सेट किया जाना चाहिए:

year.dataSource = self 
11

वास्तव में UIPickerViewDataSource प्रोटोकॉल में एक विधि है यही कारण है, तो क्या आप वाकई सेट अपने पिकर देखने के बनाने के लिए चाहता हूँ dataSource संपत्ति भी: year.dataSource = self। स्विफ्ट देशी तरीका इस तरह, वर्ग एक्सटेंशन में प्रोटोकॉल लागू करने के लिए हो रहा है:

class ExampleClass: UIViewController { 
    // properties and methods, etc. 
} 

extension ExampleClass: UIPickerViewDataSource { 
    // two required methods 

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

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

extension ExampleClass: UIPickerViewDelegate { 
    // several optional methods: 

    // func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat 

    // func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat 

    // func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! 

    // func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString! 

    // func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView! 

    // func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) 
} 
+1

एक विस्तार का उपयोग लागू करने के लिए एक प्रोटोकॉल अच्छा है क्योंकि यह सब एक ही स्थान पर प्रोटोकॉल से संबंधित कोड समाहित है, और यदि आप भविष्य में इसके बारे में अपना मन बदलो, आपको पता है कि आप क्या हटा सकते हैं :) – Jiaaro

+0

बिल्कुल! और चूंकि '# प्रगमा' चला गया है और '// मार्क ... 'विधि अभी तक काम नहीं करती है, इस समय चीजों को व्यवस्थित करने का यह एकमात्र तरीका है। ;) –

+0

नेट। इस तरह से मैं यह सुनिश्चित कैसे करूं कि मेरा यूआईपीकर व्यू उपयोग किया जा रहा है? – user3723426

संबंधित मुद्दे