首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PickerView不工作

PickerView不工作
EN

Stack Overflow用户
提问于 2013-12-10 15:02:24
回答 3查看 164关注 0票数 1

我正在使用UIPickerView从选定的数据中进行挑选。

代码语言:javascript
复制
- (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];
}

代码语言:javascript
复制
 (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;
 }

当用户点击label(label tap Event)时,我将视图上移:

代码语言:javascript
复制
-(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 !!!!!");}
                 }];

 }

但当我点击label UIPickerView display时,我无法从中提取数据。它只是显示UIPickerView。

EN

回答 3

Stack Overflow用户

发布于 2013-12-10 15:06:31

您还需要设置数据源。

代码语言:javascript
复制
myPickerView.datasource = self;
票数 3
EN

Stack Overflow用户

发布于 2013-12-10 15:12:50

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

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

票数 2
EN

Stack Overflow用户

发布于 2013-12-10 15:14:34

1)需要在ViewController的header中定义UIPickerViewDatasource

代码语言:javascript
复制
@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20488135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档