带有DONE按钮的UIPickerView [英] UIPickerView with DONE button

查看:120
本文介绍了带有DONE按钮的UIPickerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIPickerView in my application, and I want to have a done button on it to hide the picker.

我不喜欢我不想使用动作表,因为我希望专注于视图的其余部分。我的意思是所有视图都必须处于活动状态。

I don't want to use an action sheet because I want focus on the rest of the view.i mean all the view has to be active.

这是我创建的方式选择器:

Here is how I create the picker:

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


推荐答案

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

try out the custom picker make with action sheet

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;

}

}

这篇关于带有DONE按钮的UIPickerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆