2011-12-23 83 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

嘗試自定義選擇器使用行動表

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 2011-12-23 07:06:21

+1

是的,我已經在我的許多應用程序實施 – Hiren 2011-12-23 07:08:48

+1

在這裏相同。這種方法也適用於我的應用程序。 – Raptor 2013-08-07 10:23:11

7

選擇器視圖應該在哪裏你進入該國被設置爲文本字段的inputView。您應該創建一個帶有標準完成系統項目的工具欄,並將其設置爲同一字段的inputAccessoryView

1

我甚至會嘗試使用類似EAActionSheetPicker的東西。它管理一個actionSheet內部的pickerView/datePicker的顯示。它可能就是你要找的東西。

+0

儘量避免只有鏈接的答案。也許一小段代碼將幫助OP。 – Raptor 2013-08-07 10:22:44

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 2014-02-28 13:03:15