2011-12-23 29 views
3

私のアプリケーションにはUIPickerViewがあり、ピッカーを隠すためにその上に完了ボタンを付ける必要があります。完了ボタン付きUIPickerView

私は残りのview.iに焦点を当てたいので、アクションシートを使用したくないのは、すべてのビューがアクティブでなければならないということです。

UIPickerView *pickerViewCountry = [[UIPickerView alloc] init]; 
    pickerViewCountry.showsSelectionIndicator = YES; 
    pickerViewCountry.dataSource = self; 
    pickerViewCountry.delegate = self; 
    pickerViewCountry.frame = CGRectMake(0,210 , 320, 15); 
    [self.view addSubview:pickerViewCountry]; 
    [pickerViewCountry release]; 

答えて

2

あなたはUIPickerViewのアニメーションで行わボタンでUIToolBarを追加することによってこれを行うことができます:ここでは

は、私はピッカーを作成する方法です。

10

ピッカービューでは、国を入力するテキストフィールドのinputViewとして設定する必要がありますに。アクションシートで

UIActionSheet *actionSheet; 
NSString *pickerType; 
-(IBAction)BtnPressed:(id)sender 
{ 
    [self createActionSheet]; 
    pickerType = @"picker"; 
    select = NO; 
    UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)]; 
    chPicker.dataSource = self; 
    chPicker.delegate = self; 
    chPicker.showsSelectionIndicator = YES; 
    [actionSheet addSubview:chPicker]; 
    sessoTxt.text = [sessoArray objectAtIndex:0]; 
    [chPicker release]; 
} 
- (void)createActionSheet { 
    if (actionSheet == nil) { 
     // setup actionsheet to contain the UIPicker 
     actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select" 
                delegate:self 
             cancelButtonTitle:nil 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 

     UIToolbar *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]; 
     [flexSpace release]; 

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

     [pickerToolbar setItems:barItems animated:YES]; 
     [barItems release]; 

     [actionSheet addSubview:pickerToolbar]; 
     [pickerToolbar release]; 

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



#pragma mark UIPickerViewDelegate Methods 

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    int count; 
    if ([pickerType isEqualToString:@"picker"]) 
     count = [array count]; 
return count; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    NSString *string; 

    if ([pickerType isEqualToString:@"picker"]) 
     string = [array objectAtIndex:row]; 

return string; 
} 

// Set the width of the component inside the picker 
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    return 300; 
} 

// Item picked 
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    select = YES; 
    if ([pickerType isEqualToString:@"picker"]) 
    { 
     Txt.text = [array objectAtIndex:row]; 
    } 
} 

- (void)pickerDone:(id)sender 
{ 
    if(select == NO) 
    { 
     if ([pickerType isEqualToString:@"picker"]) 
     { 
      Txt.text = [array objectAtIndex:0]; 
     } 
} 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
    [actionSheet release]; 
    actionSheet = nil; 

} 

} 
+1

アクション_sheet_? :) – jrturton

+1

はい私はすでに多くのアプリで実装しています – Hiren

+1

同じです。このアプローチは私のアプリでも働いています。 – Raptor

7

を作るカスタムピッカーを試してみます標準のDoneシステム項目を持つツールバーを作成し、同じフィールドのinputAccessoryViewとして設定する必要があります。

1

EAActionSheetPickerのようなものを試してみます。これは、actionSheet内のpickerView/datePickerの表示を管理します。それはちょうどあなたが探しているものかもしれません。

+0

リンクだけで回答を避けるようにしてください。おそらく短いコードがOPを助けるでしょう。 – Raptor

9

次のようなUIPickerViewにツールバーを追加することができます。確かに、

// initiaize picker view 
UIPickerView *pickerView=[[UIDatePicker alloc]init];//Date picker 
pickerView.frame=CGRectMake(0,44,320, 216); 
pickerView.datePickerMode = UIDatePickerModeDate; 
[pickerView setMinuteInterval:5]; 
[self.view addsubview:pickerView] 

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]; 
[pickerView addSubview:toolBar]; 
+1

Latika Tiwariさんは、日付ピッカー表示を非表示にするためのchangeDateFromLabelメソッドのコーディングを教えていただけますか? – user3182143

関連する問題