点击按钮时显示UIPickerView

时间:2012-01-13 14:30:46

标签: objective-c ios cocoa-touch

标题完全解释了我此时尝试管理的内容。简短,我喜欢here的例子。只有一个问题,在这个例子中使用UIDatePicker而不是普通的UIPickerView。我的问题是我无法使用NSMutableArray的数据输入我的UIPickerView。我尝试使用三种基本方法以“滚筒”运行来共同完成它。

现在在viewDidLoad中:self.uiPickerViewData = [[NSMutableArray alloc] initWithObjects:@“One”,@“Two”,nil];

//when tapping on Set Quality button
- (IBAction)setQuality:(id)sender 
{

actionSheet=[[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleDefault];

CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
qualityPicker=[[UIPickerView alloc]initWithFrame:pickerFrame];
qualityPicker.delegate=self;
[actionSheet addSubview:qualityPicker];

[qualityPicker release];

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, actionSheet.bounds.size.width, 44)];
[toolBar setBarStyle:UIBarStyleDefault];
[toolBar sizeToFit];

UIBarButtonItem *spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *setButton=[[UIBarButtonItem alloc]initWithTitle:@"Set" style:UIBarButtonSystemItemCancel target:self action:@selector(setQualityIntoDB)];
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];

[toolBar setItems:[NSArray arrayWithObjects:spacer,setButton,cancelButton, nil]animated:NO];
[spacer release];
[setButton release];
[cancelButton release];

[actionSheet addSubview:toolBar];
[toolBar release];

[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

并没有给我带来预期结果的必需品。

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

- (NSInteger)numberOfRowsInComponent:(NSInteger)component
{
   return [uiPickerViewData count];
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [uiPickerViewData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// Will be properly implemented later

DBAccess *access=[[DBAccess alloc]init];
[access closeDataBase];
[access release];
}

我的UIPickerView为空。问题出在哪里?

2 个答案:

答案 0 :(得分:1)

在视图下方的可见性之外创建选择器和工具栏后,请使用此方法:coordinates =(0,640)):

- (void)showPicker{

[UIView beginAnimations:@"picker" context:nil];
[UIView setAnimationDuration:0.5];

    picker.transform = CGAffineTransformMakeTranslation(0,- pickerHeight);
    pickerToolbar.transform = CGAffineTransformMakeTranslation(0, - pickerHeight - toolbarHeight);

[UIView commitAnimations];  }

-(void)hidePicker{
[UIView beginAnimations:@"picker" context:nil];
[UIView setAnimationDuration:0.5];

picker.transform = CGAffineTransformIdentity;
pickerToolbar.transform = CGAffineTransformIdentity;

[UIView commitAnimations]; }

然后将showPicker添加为按钮的操作,将hidePicker添加为您想要的其他操作的动作..

答案 1 :(得分:0)

-(void)viewDidLoad
{     PickerList = [[NSMutableArray alloc] init];
            SecondPickerList = [[NSMutableArray alloc]initWithObjects:@"0.0 cm",@"0.1 cm",@"0.2 cm",@"0.3 cm",@"0.4 cm",@"0.5 cm",@"0.6 cm",@"0.7 cm",@"0.8 cm",@"0.9 cm", nil];        

            for(int i=0;i<210;i++)
            {

                [PickerList addObject:[NSString stringWithFormat:@"%i cm",i]];
            }
  [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

我的意思是你必须在didload或App Delegate中分配你的Mutablearray ......