2012-08-01 19 views
0

在我的應用中,當按下按鈕時,我將啓動UIActionSheet,其中UIPickerViewUIToolBar位於其中。它的工作完美通過使用下面的代碼。 我想獲取選擇器值並將該值設置爲被調用按鈕,但我找不到確切的解決方案。如何從iOS中的操作表中獲取pickervalue?

- (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];  
} 

這裏是我的問題

如何從動作片的選擇器值?

如何將該值設置爲按鈕?

感謝您的幫助球員

回答

1

假設pickerView數據源只有一個組件,您可以使用獲得所選行:

NSInteger row = [pickerView selectedRowInComponent:0]; 
+0

感謝您的答案..我想在'doneButtonPressed'方法中使用它..以及如何從此phik中獲取選擇器值..謝謝 – GoCrazy 2012-08-01 12:47:19

+0

是的,在doneButtonPressed中做到了這一點。該值在數據源中,所以使用'[self pickerView:pickerView titleForRow:row forComponent:0]'。 – Felix 2012-08-01 12:50:33

+0

感謝您的支持..對不起,接受剛纔的答案有一個更改來測試我的代碼..謝謝+1 – GoCrazy 2012-08-02 10:29:19

1

只需創建一個在您的.h或.M一個實例變量,並分配給[pickerView date]伊娃,你與正從ActionSheet值來完成。

編碼快樂:)

編輯

-(void)doneButtonPressed:(id)sender方法

只是把這個行代碼

selected = [pickerView date]; 

而且selected是.H聲明如下

NSDate型伊娃
@property (strong, nonatomic) NSDate *selected; 

而且要聲明一個屬性也日期選擇器

@property (strong, nonatomic) UIDatePicker *pickerView; 

而且在.M合成它,如下

@synthesize pickerView; 
@synthesize selected; 

編碼快樂:)

編輯1

對於定製撿拾器查看

(I假設所有的代表和用於pickerView數據源被定義,並宣佈)

獲得選自定製選擇器值必須使用下面提到的代碼

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

singlePicker = pickerView和pickerDataNSArray它被用作picker的數據源查看

這可以幫助你解決你的問題。

編碼快樂:)

+0

謝謝您的回答。對不起,我是新來的iOS和不能得到你..什麼是[pickerView日期],如果可能的話,你可以說如何設置文本按鈕 – GoCrazy 2012-08-01 11:19:40

+0

好吧會把它的一些代碼很快 – 2012-08-01 11:21:12

+0

我很抱歉,我沒有使用任何日期在這裏.. pickerview值是從NSArray ..你將如何得到'選擇= [pickerView日期]'從我的代碼..謝謝 – GoCrazy 2012-08-01 12:45:20

相關問題