IOS实现自定义时间UIPickerView Popup问题

Posted

技术标签:

【中文标题】IOS实现自定义时间UIPickerView Popup问题【英文标题】:Implementing Custom time UIPickerView Popup issue IOS 【发布时间】:2013-06-06 20:26:30 【问题描述】:

编辑:忘记分配委托,现在可以使用。

    self.timePickerView.delegate = self;
        self.timePickerView.dataSource = self;

好的,所以我使用 UIDatePicker 进行了此操作,并且效果很好,但是我想包含秒数,因此不得不尝试使用自定义选择器。现在我的操作表和按钮弹出,但我的自定义选择器应该是黑色的,我知道我错过了一些东西,或者只是采取了错误的方法。目标是让用户使用自定义选择器来选择一个小时/分钟/秒来记录他们在跑步上花费的时间等.....

我的主要问题是如何让我的自定义选择器在单击文本字段时出现,我知道我在选择器本身的实现中遗漏了一些东西,也许这个自定义选择器不遵循组件的默认方法/行?我知道我之前使用的日期不需要那些?提前感谢您提供任何见解。

标题

@property (strong, nonatomic) UIPickerView *timePickerView;
@property (strong, nonatomic) UIActionSheet *datePickerViewPopup;
@property (retain, nonatomic) NSMutableArray *hoursArray;
@property (retain, nonatomic) NSMutableArray *minutesArray;
@property (retain, nonatomic) NSMutableArray *secondsArray;

视图确实加载了

       NSString *stringValue = [[NSString alloc]init];
self.hoursArray = [[NSMutableArray alloc]init];
self.minutesArray = [[NSMutableArray alloc]init];
self.secondsArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 61; i++) 
    stringValue = [NSString stringWithFormat:@"%d", i];
    if (i < 13) 
        [self.hoursArray addObject:stringValue];
    
    [self.minutesArray addObject:stringValue];
    [self.secondsArray addObject:stringValue];

调用我的弹出窗口的单元格

    - (void)textFieldDidBeginEditing:(UITextField *)textField

   switch (textField.tag) 
    case 1003:
        [textField resignFirstResponder];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
        [dateFormatter setDateFormat:@"HH:mm:ss"];

        self.datePickerViewPopup = [[UIActionSheet alloc]init];
        const CGFloat toolBarHeight = 44.0f;
        self.timePickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, toolBarHeight, 0, 0)];
        self.timePickerView.hidden = NO;

        UIToolbar *pickerToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, toolBarHeight)];
        pickerToolbar.barStyle = UIBarStyleBlackOpaque;
        [pickerToolbar sizeToFit];
        NSMutableArray *toolBarItems = [[NSMutableArray alloc]init];

        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
        [toolBarItems addObject:cancelButton];

        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [toolBarItems addObject:flexSpace];

        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
        [toolBarItems addObject:doneButton];

        NSDateComponents *comps = [[NSDateComponents alloc]init];
        comps.hour = [self.timePickerView selectedRowInComponent:0];
        comps.minute = [self.timePickerView selectedRowInComponent:1];
        comps.second = [self.timePickerView selectedRowInComponent:2];
        NSCalendar *calerdar = [NSCalendar currentCalendar];
        NSDate *cardioTime = [calerdar dateFromComponents:comps];

        textField.text = [dateFormatter stringFromDate:cardioTime];

        [pickerToolbar setItems:toolBarItems animated:NO];

        [self.datePickerViewPopup addSubview:pickerToolbar];
        [self.datePickerViewPopup addSubview:self.timePickerView];
        [self.datePickerViewPopup showInView:self.view.superview];
        [self.datePickerViewPopup setBounds:CGRectMake(0, 0, self.view.frame.size.width, 464)];

        break;

选择器方法

   #pragma mark - Picker Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

return 3;


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

if (component == 0) 
    return [self.hoursArray count];
 else if (component == 1) 
    return [self.minutesArray count];
 else 
    return [self.secondsArray count];




- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

switch (component) 
    case 0:
        return [self.hoursArray objectAtIndex:row];
        break;
    case 1:
        return [self.minutesArray objectAtIndex:row];
        break;
    case 2:
        return [self.secondsArray objectAtIndex:row];
    default:
        break;

return nil;

【问题讨论】:

【参考方案1】:

您无需创建操作表。只需创建 UIPickerView 并将其分配给文本字段 inputView

【讨论】:

感谢您的回复,但据我所知,如果我这样做,我会丢失我想出现并与选择器一起使用的取消和完成按钮。我确实找到了解决方案,正如我所怀疑的那样,我是个菜鸟。干杯【参考方案2】:

我只是一个菜鸟,我在初始化选择器后忘记添加以下内容。现在我只需要清理我剩下的烂摊子。

    self.timePickerView.delegate = self;
        self.timePickerView.dataSource = self;

【讨论】:

以上是关于IOS实现自定义时间UIPickerView Popup问题的主要内容,如果未能解决你的问题,请参考以下文章

iOS 7 中的自定义 UIPickerView 崩溃

带有自定义视图的 UIPickerView 在 iOS7 中被破坏

iOS Cocoa - 自定义 uipickerview 或类似

iOS7 UIPickerView 的奇怪行为与 UIImage 作为自定义视图,任何官方信息?

如何在 iOS 应用程序中通过 UIPickerView 实现视图之间的转换?

iOs5,试图了解 UIPickerView 以及如何将其连接到我的自定义类