Iphone 带“完成”按钮的UIPickerView

Iphone 带“完成”按钮的UIPickerView,iphone,objective-c,ios,Iphone,Objective C,Ios,我的应用程序中有一个UIPickerView,我想在上面有一个“完成”按钮来隐藏选择器 我不想使用操作表,因为我想集中在视图的其余部分。我的意思是所有视图都必须处于活动状态 以下是我如何创建选择器: UIPickerView *pickerViewCountry = [[UIPickerView alloc] init]; pickerViewCountry.showsSelectionIndicator = YES; pickerViewCountry.dataSource =

我的应用程序中有一个UIPickerView,我想在上面有一个“完成”按钮来隐藏选择器

我不想使用操作表,因为我想集中在视图的其余部分。我的意思是所有视图都必须处于活动状态

以下是我如何创建选择器:

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

您可以通过添加一个带有UIPickerView动画的
UIToolBar
with Done按钮来完成此操作。

尝试使用操作表制作自定义选择器

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;

}

}

选择器视图应设置为输入国家/地区的文本字段的
inputView
。您应该创建一个带有标准完成系统项的工具栏,并将其设置为同一字段的
inputAccessoryView

我甚至会尝试使用类似的东西。它管理操作表中pickerView/datePicker的显示。它可能正是您想要的。

您可以向
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];

尽量避免只使用链接的答案。也许一段简短的代码会对OP.Latika Tiwari有所帮助。你能给出隐藏日期选取器视图的changeDateFromLabel方法编码吗?对于阅读本文的任何人,从iOS8开始,将UIPicker放入操作表实际上会破坏应用程序。这在一段时间内被认为是一种黑客行为。