UIPickerView& DatePicker行为异常

时间:2017-05-30 10:36:31

标签: objective-c uipickerview uidatepicker xcode8

我正在将一个项目从spring和struts转换为storyBoard。有一个VC,登录后,用户必须初始化个人资料信息。我有四个名字,dob,性别和城市的字段。在性别和dob上我分别实现了pickerView和DateView,以便点击文本字段选择器/日期视图,以便用户可以从中进行选择。我已将它们从屏幕底部固定,并且未将它们隐藏在storyboard属性的检查器中。它是在代码中手动完成的。

在测试时,我遇到了一些麻烦,因为datePicker行为异常,如果在任何其他位置点击它,并且pickerView也没有选择完成按钮操作上的选项。 pickerView的委托和数据源已连接。它在弹簧和支柱中使用相同的代码完美地工作。我想知道在这里我可能做错了什么,如果有一个简单的方法隐藏我想要的输出隐藏视图。

这是showDatePickerView方法

-(void) showDatePickerView {

if (self.dob != nil) {
    [self.datePickerView setDate:self.dob];
}

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.datePickerView.backgroundColor = [UIColor groupTableViewBackgroundColor];

CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
//Origin will be screen size - picker height
float origin = iOSDeviceScreenSize.height - 256;
CGRect frame = self.datePickerView.frame;
frame.origin.y = origin;
self.datePickerView.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = origin-44;
self.toolBar.frame = frame;

[UIView commitAnimations];
}

这是hideDatePickerViewMethod

-(void) hideDatePickerView {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

CGRect frame = self.datePickerView.frame;
frame.origin.y = iOSDeviceScreenSize.height;
self.datePickerView.frame = frame;
frame = self.toolBar.frame;
frame.origin.y = iOSDeviceScreenSize.height;
self.toolBar.frame = frame;
[self.view setUserInteractionEnabled:YES];
[UIView commitAnimations];
}

像这样我还实现了show pickerView并隐藏了在textField“textFieldShouldBeginEditing”的委托方法中调用的pickerView方法

1 个答案:

答案 0 :(得分:1)

让我告诉你一种针对这种情况的不同而简单的方法:

在界面中的pickerView上创建如下:

UIPickerView *pickerView;
UITextField  *activeTextField; //To keep the reference of activeTextField

然后在ViewDidLoad()写:

pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];
pickerView.dataSource = self;
pickerView.delegate = self;

现在您已初始化了pickerView

以下是UIPickerViewDataSourceUIPickerViewDelegate方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2; //Whatever number of Components you want
}


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

    if (component==0)
    {
        return [YOUR_ARRAY count];
    }
    else
    {
        return [YOUR_ARRAY count];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

//You can even update your UITextfield if you want or whatever the UIElement you want to update
    //UITextField* nextTextField;

     //if (activeTextField.tag == 301) {

     //} else if(activeTextField.tag == 302)  {

   //}
}



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

    switch (component)
    {
        case 0:
            return [YOUR_ARRAY objectAtIndex:row];
            break;
        case 1:
            return [YOUR_ARRAY objectAtIndex:row];
            break;

    }
    return nil;
}

现在在textFieldShouldBeginEditing写:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {       // return NO to disallow editing. {

activeTextField=textField;
//Now check for which textField you want to show pickerView
if activeTextField == YOUR_TEXTFIELD {

textField.inputView=pickerView;
 }
}

如果您发现任何困难或任何额外功能,请告诉我们!