如何从iOS中的操作表中获取pickervalue?

时间:2012-08-01 08:00:15

标签: ios uibutton uipickerview uiactionsheet

在我的应用中,当按下按钮时,我正在UIActionSheet启动UIPickerViewUIToolBar。它使用以下代码完美地工作。 我正在尝试获取选择器值并将该值设置为被调用按钮,但我找不到确切的解决方案。

- (IBAction)showPicker:(id)sender {

 actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];//as we want to display a subview we won't be using the default buttons but rather we're need to create a toolbar to display the buttons on 

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

pickerView = [[UIPickerView     alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
//[pickerView release];     

pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];

NSMutableArray *barItems = [[NSMutableArray alloc] init];   

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
[barItems addObject:flexSpace];  

UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
[barItems addObject:doneBtn];  

UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
[barItems addObject:cancelBtn];  


[pickerToolbar setItems:barItems animated:YES];  

[actionSheet addSubview:pickerToolbar];  

[actionSheet addSubview:pickerView];  

[actionSheet showInView:self.view];  

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];         

}

当按下工具栏中的完成按钮时,它将调用此方法

-(void)doneButtonPressed:(id)sender{  
//Do something here here with the value selected using [pickerView date] to get that value   
[actionSheet dismissWithClickedButtonIndex:1 animated:YES];   
[pickerView release];
[pickerToolbar release];
[actionSheet release];     
}  

这是我的问题

如何从操作表中获取选择器值?

如何将该值设置为按钮?

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

只需在.h或.m中创建一个ivar,然后将[pickerView date]分配给ivar,您就可以从ActionSheet中获取值。

快乐编码:)

修改

在你的-(void)doneButtonPressed:(id)sender方法中放入这行代码

selected = [pickerView date];

selected是.h中声明的NSDate类型ivar,如下所示

@property (strong, nonatomic) NSDate *selected;

还必须为datePicker声明一个属性

@property (strong, nonatomic) UIDatePicker *pickerView;

在.m中合成它如下

@synthesize pickerView;
@synthesize selected;

快乐编码:)

修改1

对于自定义pickerView

(我假设定义并声明了pickerView的所有委托和数据源)

从自定义选择器中获取选定值必须使用下面提到的代码

NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row]; 

singlePicker = pickerView和pickerDataNSArray,用作pickerView的数据源

这可以帮助您解决问题。

快乐编码:)

答案 1 :(得分:1)

假设pickerView数据源只有一个组件,您可以使用以下方法获取所选行:

NSInteger row = [pickerView selectedRowInComponent:0];