延迟一段时间后显示uipickerview

时间:2016-06-09 19:44:27

标签: ios objective-c uipickerview

我有uitextfield并添加了选择器视图作为输入视图。

一切都很完美。我的问题是,一旦我点击uitextfield选择器视图出现值。但我从数据库中获取了一些数据,这些数据将根据所选的选择器值更新为另一个文本框。

从数据库获取数据时有5-6秒的时间间隔。但是在调用返回数据之前,选择器视图与预加载值一起可见。

我只是想添加一种延迟,直到我从数据库选择器值获取数据不应该是可见的... 注意:我已经使用了隐藏。不能正常工作。

UIPickerView  *vwlistPicker = [[UIPickerView alloc] init];
[vwlistPicker sizeToFit];
vwlistPicker.delegate = self;
vwlistPicker.dataSource = self;
vwlistPicker.showsSelectionIndicator = YES;

UITextField  *txtPayType = [[UITextField alloc] initWithFrame:CGRectMake(0, 65, 250, 30)];
txtPayType.layer.borderWidth = 0.5;
txtPayType.layer.masksToBounds = true;
txtPayType.layer.cornerRadius = 5;
txtPayType.layer.borderColor = [[UIColor blackColor] CGColor];
txtPayType.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
txtPayType.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtPayType.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
txtPayType.tag = 1;
[txtPayType setDelegate:self];
txtPayType.text = [tempDic valueForKey:@"PayType"];
[txtPayType setFont:[UIFont fontWithName:@"Avenir-Medium" size:15.0]];
[txtPayType setTextColor:[UIColor colorWithRed:19.0/255.0 green:62.0/255.0 blue:137.0/255.0 alpha:1]];
[txtPayType setReturnKeyType:UIReturnKeyDone];
txtPayType.inputView = vwlistPicker;

UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleDefault;
keyboardDoneButtonView.translucent = NO;
keyboardDoneButtonView.barTintColor = [UIColor colorWithRed:10.0/255.0 green:23.0/255.0 blue:75.0/255.0 alpha:0];

[keyboardDoneButtonView sizeToFit];

UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStylePlain target:self
                                                               action:@selector(itemPickerDoneClicked:)] autorelease];

UIBarButtonItem* cancelButton = [[[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(itemPickerCancelClicked:)] autorelease];


[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton,cancelButton, nil]];

txtPayType.inputAccessoryView = keyboardDoneButtonView;

[self.view addSubview:txtPayType];

//Textfield delegate

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        if(textField.tag == 1)
        {
            [vwlistPicker setHidden:TRUE];
            double delayInSeconds = 5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [vwlistPicker reloadInputViews];
                textField.inputView = vwlistPicker;
                [vwlistPicker reloadAllComponents];
                [vwlistPicker setHidden:FALSE];
            });
// Call to DB and get Data //
    }

我添加了延迟/隐藏(键盘和pickerview),但仍然出现键盘。并且在出现延迟选择器视图选项后。

我不希望显示任何内容,一旦我获得数据选择器视图就会出现。

1 个答案:

答案 0 :(得分:1)

您应该实现UITextField委托方法textFieldShouldBeginEditing,并为需要网络数据的文本字段返回FALSE以填充选择器。你可以像Teja建议的那样在窗口顶部显示一个进度指示器,然后隐藏进度" spinner"并在文本字段上调用becomeFirstResponder,以便在下载完成后启用编辑。