使用UIPickerview作为iOS 8中UITextfield的输入视图时崩溃

时间:2014-11-13 01:08:22

标签: ios objective-c uipickerview

我一直在使用UIPickerview作为UITextfield的输入,它完全适用于iOS 7,但是一旦我在iOS 8上运行它,只要我选择文本字段输入数据就会崩溃应用程序;我不知道为什么。

这是代码。

这是我的头文件

 @interface myViewController:UIViewController
 <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
 @property (weak, nonatomic) IBOutlet UITextField *myTxtfield;

 @end

这是我的.m文件

 @implementation myViewController

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     NSString *filePath = [[NSBundle mainBundle]
     pathForResource:@"myData" ofType:@"plist"];

     data = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
     self.pickerView.showsSelectionIndicator = YES;
 }

 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
      return 1;
 }

 -(NSInteger)pickerView:(UIPickerView *)pickerView
 numberOfRowsInComponent:(NSInteger)component{
     return [data count];
 }

 -(NSString *)pickerView:(UIPickerView *)pickerView
  titleForRow:(NSInteger)row forComponent:(NSInteger)component{
      return [data objectAtIndex:row];
 }

 - (void)pickerView:(UIPickerView *)pickerView
 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 {
     currentTextField.text = [data objectAtIndex:row];
 }


 -(BOOL) textFieldShouldReturn:(UITextField *)textField{


     [_myTxtfield resignFirstResponder];

     return YES;
 }

 -(void)textFieldDidBeginEditing:(UITextField *)textField {

     if (_myTxtfield.isEditing==YES)
     {
         textField.inputView = _pickerView;
         currentTextField = textField;
      }

    [[textField valueForKey:@"textInputTraits"]
    setValue:[UIColor lightGrayColor] forKey:@"insertionPointColor"];
 }

 -(void)textFieldDidEndEditing:(UITextField *)textField {
             [_pickerView selectRow:0 inComponent:0 animated:NO];
 }

 @end

这是崩溃消息

 *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<UICompatibilityInputViewController: 0x7fa7fd412c40> should have parent view controller:<myViewController: 0x7fa7fb8b7600> but requested parent is:<UIInputWindowController: 0x7fa7fd87d600>'
 *** First throw call stack:
 (
     0   CoreFoundation                      0x0000000105521f35 __exceptionPreprocess + 165
     1   libobjc.A.dylib                     0x00000001051babb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000105521e6d +[NSException raise:format:] + 205
    3   UIKit                               0x000000010394889f -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 184
    4   UIKit                               0x0000000103ef5308 -[UIInputWindowController changeToInputViewSet:] + 491
    5   UIKit                               0x0000000103ef5ebd __43-[UIInputWindowController setInputViewSet:]_block_invoke + 85
     6   UIKit                               0x00000001038915ce +[UIView(Animation) performWithoutAnimation:] + 65
     7   UIKit                               0x0000000103ef5c8e -[UIInputWindowController setInputViewSet:] + 291
     8   UIKit                               0x0000000103ef1a7a -[UIInputWindowController performOperations:withAnimationStyle:] + 50
     9   UIKit                               0x0000000103ce1bce -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1054
     10  UIKit                               0x000000010399731d -[UIResponder becomeFirstResponder] + 468
     11  UIKit                               0x000000010388ce03 -[UIView(Hierarchy) becomeFirstResponder] + 99
     12  UIKit                               0x0000000103f53ad7 -[UITextField becomeFirstResponder] + 51
     13  UIKit                               0x0000000103bdb9c1 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 177
     14  UIKit                               0x0000000103bdda30 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 2263
     15  UIKit                               0x0000000103bd32e6 _UIGestureRecognizerSendActions + 262
     16  UIKit                               0x0000000103bd1f89 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532
     17  UIKit                               0x0000000103bd6ba6 ___UIGestureRecognizerUpdate_block_invoke662 + 51
     18  UIKit                               0x0000000103bd6aa2 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254
     19  UIKit                               0x0000000103bccb1d _UIGestureRecognizerUpdate + 2796
     20  UIKit                               0x0000000103866ff6 -[UIWindow _sendGesturesForEvent:] + 1041
     21  UIKit                               0x0000000103867c23 -[UIWindow sendEvent:] + 667
     22  UIKit                               0x00000001038349b1 -[UIApplication sendEvent:] + 246
     23  UIKit                               0x0000000103841a7d _UIApplicationHandleEventFromQueueEvent + 17370
    24  UIKit                               0x000000010381d103 _UIApplicationHandleEventQueue + 1961
     25  CoreFoundation                      0x0000000105457551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
     26  CoreFoundation                      0x000000010544d41d __CFRunLoopDoSources0 + 269
     27  CoreFoundation                      0x000000010544ca54 __CFRunLoopRun + 868
    28  CoreFoundation                      0x000000010544c486 CFRunLoopRunSpecific + 470
    29  GraphicsServices                    0x00000001082cc9f0 GSEventRunModal + 161
    30  UIKit                               0x0000000103820420 UIApplicationMain + 1282
    31  أحسب نسبتك               0x0000000102a7afbd أحسب نسبتك + 8125
    32  libdyld.dylib                       0x0000000105f11145 start + 1
 )  

 libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:0)

我使用这个新代码解决了这个错误

 //in .h file

  //CHANGED WEAK PROPERTY TO RETAIN
  @property (retain, nonatomic) IBOutlet UIPickerView *pickerView;

 //in .m file
     //ADDED THESE LINES IN VIEWDIDLOAD
     _pickerView = [[UIPickerView alloc]init];
     _pickerView.delegate = self;
     _pickerView.dataSource = self;
相关问题