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

Posted

技术标签:

【中文标题】在 iOS 8 中使用 UIPickerview 作为 UITextfield 的输入视图时崩溃【英文标题】:Crash when using UIPickerview as inputview for UITextfield in iOS 8 【发布时间】:2014-11-13 01:08:21 【问题描述】:

我一直在使用 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】:

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

 //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;

【讨论】:

如果您突出显示(或总结)您的解决方案将是最好的 刚刚总结了解决办法

以上是关于在 iOS 8 中使用 UIPickerview 作为 UITextfield 的输入视图时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

带有 UIActionSheet IOS 8 的 UIPickerView 停止工作

UIPickerView 在 IOS 8 中不起作用

在 iOS 应用中使用 UIPickerView 时不需要采用数据源?

ios 7 中带有一个组件的自定义 UIPickerView

在 iOS 7 中重用 UIPickerView 中的视图

如何在iOS 7下更改UIPickerView中文本的颜色?