PickerView无法正常工作

时间:2013-12-10 07:02:24

标签: ios iphone ios7

我正在使用UIPickerView从所选数据中挑选。

- (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。

3 个答案:

答案 0 :(得分:3)

您还需要设置数据源。

myPickerView.datasource = self;

答案 1 :(得分:2)

.h 文件中,请确保添加了UIPickerViewDataSource& UIPickerViewDelegate .m 文件,picker.dataSource = Self;&分配picker.delegate = Self对象后,UIPickerView

注意: - 我无法对此问题发表评论,这就是为什么提到这个答案。

答案 2 :(得分:0)

1)您需要在ViewController的标题中定义 UIPickerViewDatasource

@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>

2)在- (void)viewDidLoad中,您需要myPickerView.datasource = self;myPickerView.delegate = self;进行{{1}}

3)我希望您填充数据源,即 ary 数组,因为我无法在您粘贴的代码中看到该部分。