2013-03-01 82 views
0

我試圖創建UITextField動態然後點擊textField試圖顯示選擇器視圖,但一些如何獲得上述錯誤。 它運行循環錯誤不會停止,除非我停止應用程序。UIApplication beginIgnoringInteractionEvents]溢出。忽略

代碼

-(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

下面介紹一些簡單的代碼顯示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]; 
    }