iphone - 带有 DONE 按钮的 UIPickerView

标签 iphone objective-c ios

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

我不想使用操作表,因为我想关注 View 的其余部分。我的意思是所有 View 都必须处于事件状态。

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

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

最佳答案

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

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;

}

}

关于iphone - 带有 DONE 按钮的 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8613009/

相关文章:

iPhone - 将字典写入文件 : handling errors

iphone - 中文环境默认字体是什么?

iphone - 带 OpenGL 着色器的高斯滤波器

ios - 如何为一对多关系 Core Data iOS 添加谓词

iOS 类,实现多个字符串的正确方法?

ios - LayoutConstraints - 无法同时满足约束

iphone - 使用超声波传输数据

ios - 在 RLMArray 上添加或更新对象?

ios - 检测 UITextField 自动建议文本替换

ios - self.view.addSubview 和 view.addSubview 的区别