我正在尝试动态创建UITextField
然后点击textField尝试显示选择器视图,但是有些人如何得到上述错误。
并且它在循环中运行错误不会停止,除非我停止应用程序。
代码
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
if (textField.tag < 10) {
if ([[datatypeArray objectAtIndex:textField.tag] isEqualToString:@"Currency"])
{
[self showpicker:textField];
}
else if([[datatypeArray objectAtIndex:textField.tag ] isEqualToString:@"Date" ])
{
[self showpicker:textField];
}
else if([[datatypeArray objectAtIndex:textField.tag] isEqualToString:@"Text"])
{
}
}
else
{
[self showpicker:textField];
}
}
-(void)showpicker:(UITextField *)textfield
{
[textfield resignFirstResponder];
if (textfield.tag >= 10 ) {
currentTextField=textfield;
pickerArray=arraycurrency;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UIToolbar *pickerToolbar;
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];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
else if ([[datatypeArray objectAtIndex:textfield.tag ] isEqualToString:@"Date" ])
{
[textfield resignFirstResponder];
currentTextField=textfield;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerDateView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerDateView.datePickerMode = UIDatePickerModeDate;
pickerDateView.hidden = NO;
pickerDateView.date = [NSDate date];
pickerDateView.userInteractionEnabled=YES;
pickerDateView.timeZone=[NSTimeZone localTimeZone];
UIToolbar *pickerToolbar;
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];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[actionSheet addSubview:pickerDateView];
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
}
else if ( [[datatypeArray objectAtIndex:textfield.tag] isEqualToString:@"Text"])
{
[textfield becomeFirstResponder];
}
else if((textfield.tag==0 || textfield.tag==1 || textfield.tag==2 || textfield.tag==3 || textfield.tag==4) &&
[[datatypeArray objectAtIndex:textfield.tag] isEqualToString:@"Currency"]);
{
[textfield becomeFirstResponder];
}
}
答案 0 :(得分:0)
在点击UIPickerView
UItextField
的简单代码
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
[self willDisplayPickerView];
}
//代码willDisplayPickerView
方法:
- (void)willDisplayPickerView
{
// Init the picker view.
pickerView = [[UIPickerView alloc] init];
// Set the delegate and datasource. Don't expect picker view to work
// correctly if you don't set it.
[pickerView setDataSource: self];
[pickerView setDelegate: self];
// Set the picker's frame. We set the y coordinate to 50px.
[pickerView setFrame: CGRectMake(xPoint, 50.0f, pickerWidth, 200.0f)];
// Before we add the picker view to our view, let's do a couple more
// things. First, let the selection indicator (that line inside the
// picker view that highlights your selection) to be shown.
pickerView.showsSelectionIndicator = YES;
// Allow us to pre-select the third option in the pickerView.
[pickerView selectRow:2 inComponent:0 animated:YES];
// OK, we are ready. Add the picker in our view.
[self.view addSubview: pickerView];
}