我正在使用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;
}当用户点击label(label tap Event)时,我将视图上移:
-(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。
发布于 2013-12-10 15:06:31
您还需要设置数据源。
myPickerView.datasource = self;发布于 2013-12-10 15:12:50
在.h文件中,确保在分配UIPickerView对象后添加了UIPickerViewDataSource & UIPickerViewDelegate,并在.m文件中添加了picker.dataSource = Self; & picker.delegate = Self。
注意:-我无法对这个问题发表评论,这就是为什么提到这个作为答案。
发布于 2013-12-10 15:14:34
1)需要在ViewController的header中定义UIPickerViewDatasource,
@interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>2)在- (void)viewDidLoad中,你需要用myPickerView.delegate = self;做myPickerView.datasource = self;
3)我希望您正在填充您的数据源,即ary数组,因为我在您粘贴的代码中看不到这一部分。
https://stackoverflow.com/questions/20488135
复制相似问题