【问题标题】:UIPickerView with DONE button带有完成按钮的 UIPickerView
【发布时间】:2013-06-07 13:20:36
【问题描述】:

我的应用程序中有一个 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];

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

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

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

    【讨论】:

    • 对于任何阅读本文的人来说,从 iOS8 开始,将 UIPickers 放入操作表实际上会破坏应用程序。有一段时间,这被认为是一种黑客行为。
    【解决方案2】:

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

    【讨论】:

    • Latika Tiwari 能否给 changeDateFromLabel 方法编码以隐藏日期选择器视图?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您可以通过添加带有 UIPickerView 动画的完成按钮的 UIToolBar 来实现。

      【讨论】:

        【解决方案5】:

        我什至会尝试使用EAActionSheetPicker 之类的东西。它管理 actionSheet 内的 pickerView/datePicker 的显示。它可能正是您正在寻找的。​​p>

        【讨论】:

        • 尽量避免只使用链接的答案。也许一小段代码会对 OP 有所帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多