从UIPickerView中删除未选择的项目

时间:2013-11-22 04:55:10

标签: ios objective-c cocoa-touch

我正在处理一个有多个UIPickerViews的nib文件,如下所示,它非常拥挤。我基本上想要使它干净并删除所有“灰色”项目,只显示所选项目。如果您有任何可以让页面更清晰的想法请分享,这更多是UI问题,而不是逻辑或代码问题。提前谢谢。

enter image description here

2 个答案:

答案 0 :(得分:2)

您无需在其中添加 UIPickerView 。您只需添加文本字段并将 UIPickerView 设置为文本字段的输入附件视图。因此,您的选择器将显示在底部,您可以使用委托方法在文本字段中显示选择器的选定值。

enter image description here

答案 1 :(得分:1)

确保将<UIActionSheetDelegate>添加到.h文件 我使用此代码作为日期选择器,文本字段旁边有一个按钮。无论何时单击此按钮,都会在其上的操作表中弹出。您可能需要稍微调整一下:

在您的按钮事件中添加以下代码:

UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Pick the Category" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Set Category", nil];
[asheet showInView:[self.view superview]]; //note: in most cases this would be just self.view, but because I was doing this in a tabBar Application, I use the superview.
[asheet setFrame:CGRectMake(0, 117, 320, 383)];

tagOfDateToUse = 1; //Pick any number you want to use as a tag.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
//Configure picker...
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
//Configure picker...

//Add picker to action sheet
[asheet addSubview:pickerView];

然后添加此方法:

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

 // Here is where you will put an IF check to see what tag it is and within that if
        if (tagOfDateToUse == 1)
        {
        UIDatePicker *ourDatePicker;
        ourDatePicker = [[UIDatePicker alloc] init];

        NSArray *subviews = [actionSheet subviews];
        ourDatePicker = [subviews objectAtIndex:4];


        NSDate *selectedDate = [ourDatePicker date];
 }
 }

最后:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
//Gets an array af all of the subviews of our actionSheet
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:2] setFrame:CGRectMake(20, 255, 280, 46)];
[[subviews objectAtIndex:3] setFrame:CGRectMake(20, 305, 280, 46)];   
 };

注意:您可能需要根据设置和取消按钮更改子视图ObjectAtIndex数字。

相关问题