2013-12-10 14 views
1

我正在使用UIPickerView從所選數據中進行選取。PickerView無法正常工作

- (void)viewDidLoad 
{ 

myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 640, 320, 200)]; 
myPickerView.delegate = self; 
myPickerView.showsSelectionIndicator = YES; 
[self.view addSubview:myPickerView]; 
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)]; 
[toolBar setBarStyle:UIBarStyleBlackOpaque]; 
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                    style:UIBarButtonItemStyleBordered target:self action:@selector(changeDateFromLabel:)]; 
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil]; 
barButtonDone.tintColor=[UIColor blackColor]; 
} 

(void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component { 
// Handle the selection 
NSLog(@"%d",row); 
lbl.text=[ary objectAtIndex:row]; 
} 

// tell the picker how many rows are available for a given component 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
NSUInteger numRows = 5; 

return numRows; 
} 

// tell the picker how many components it will have 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 
return 1; 
} 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ 

UILabel* tView = (UILabel*)view; 
if (!tView){ 
    tView = [[UILabel alloc] init]; 
    // Setup label properties - frame, font, colors etc 
    tView.font=[UIFont systemFontOfSize:16.0f]; 
    //adjustsFontSizeToFitWidth property to YES 
    // tView.adjustsFontSizeToFitWidth = YES; 
} 
// Fill the label text here 
tView.text=[ary objectAtIndex:row]; 
[tView setLineBreakMode:NSLineBreakByCharWrapping]; 
[tView setNumberOfLines:0]; 
return tView; 
} 

,我動視了當標籤用戶點擊(標籤自來水事件):

-(void)labelTap{ 
v1.hidden=NO; 
[UIView animateWithDuration:0.25 delay:0.0 options: UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        CGRect frm; 
        frm=self.view.frame; 
        frm.origin.y=frm.origin.y-400; 
        self.view.frame=frm; 
        // self.view.frame=CGRectMake(0, -30, 320, 200); 
        //v1.frame=CGRectMake(0, 360, 320, 200); 
        // v1.backgroundColor=[UIColor blackColor]; 
       } 
       completion:^(BOOL finished){ 
        if(finished) {NSLog(@"Finished end !!!!!");} 
       }]; 

} 

但是當我點擊標籤UIPickerView顯示屏上,但我無法從中選擇數據。它只是顯示UIPickerView。

+0

您使用tView,並使用didSelectRow:in lbl。 您使用哪個標籤? – payal

回答

3

您還需要設置數據源。

myPickerView.datasource = self; 
2

.H文件,請確保您已經添加UIPickerViewDataSource & UIPickerViewDelegate.M文件,picker.dataSource = Self; & picker.delegate = Self後你分配UIPickerView對象。

注意: - 我無法評論這個問題,這就是爲什麼提到這個答案。

+0

這不起作用 – vivek

0

1)你必須在你的ViewController的頭文件來定義UIPickerViewDatasource

@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> 

2)在- (void)viewDidLoad你需要做myPickerView.datasource = self;myPickerView.delegate = self;

3)我希望你填充您的數據源即ary array,因爲我無法在您粘貼的代碼中看到該部分。

+0

但如果我添加選擇器視圖到另一個視圖,然後將該視圖添加爲子視圖並運行應用程序,那麼它正在工作,無需轉換數據源。 – vivek

+0

和myPickerView.datasource = self;沒有爲此工作 – vivek