2011-03-21 10 views
5

यहाँ देखने मैं एक pickerview programaticlyइस पिकर एक डेटा स्रोत

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     CGRect pickerFrame = CGRectMake(0,280,321,200); 

     UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure 

     //Set up the picker view as you need it 

     //Set up the display frame 
     myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions 

     //Add the picker to the view 
     [self.view addSubview:myPickerView]; 
    } 

द्वारा जोड़ा जा रहा लेकिन अब मैं वास्तव में यह सामग्री प्रदर्शित की है और किसी भी तरह बदले, तो उसे और क्या मूल्य इसे करने के लिए बदल गया है पता लगाने के लिए की जरूरत है देने के लिए कैसे। मैं यह कैसे करु?

+0

उपयोग करने के लिए प्रतिनिधि में एक नजर है UIPickerView डेटासोर्स और प्रतिनिधियों के लिए xcode दस्तावेज़। – MCannon

उत्तर

18
ज फ़ाइल जगह में

इस कोड

@interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

डेटा स्रोत निर्दिष्ट करने तथा पिकर

// this view controller is the data source and delegate 
myPickerView.delegate = self; 
myPickerView.dataSource = self; 

निम्नलिखित प्रतिनिधि और datasouce तरीकों

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 

} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 
{ 
    return 200; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 
{ 
    return 50; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *returnStr = @""; 
    if (pickerView == myPickerView) 
    {  
     returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"]; 
    } 

    return returnStr; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (pickerView == myPickerView) 
    { 
     return [levelPickerViewArray count]; 
    } 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 
0

आपको एक कक्षा बनाने की आवश्यकता है जो UIPickerViewDataSource प्रोटोकॉल लागू करता है और इसका उदाहरण myPickerView.dataSource पर असाइन करता है।

+0

मैं आवृत्ति कैसे आवंटित करूं? – Michael

+0

@ माइकल आप बस myPickerView.dataSource = (अपने डेटासोर्स का उदाहरण) सेट करें। जहां डेटासोर्स उदाहरण आता है, वह आपके ऐप के लिए विशिष्ट है। बस याद रखें कि UIPickerView अपने डेटा स्रोत को बरकरार नहीं रखता है, इसलिए आपको इसे अपने व्यू कंट्रोलर (यदि कहीं और नहीं) में बनाना/बनाए रखना होगा। – Tony

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