3

我想在单击按钮时创建 uiview 的自定义子类(SubCustomView),并在 SubCustomView 上添加 uitextview 类作为子视图。当我启动 uitextview 我使用initWithFrame:textContainer:方法。当我使用这种方法时,项目崩溃了。当我使用initWithFrame:方法时,项目不会崩溃。我正在使用以下代码:

textContainer1 = [[NSTextContainer alloc] initWithSize:CGSizeMake(2, CGFLOAT_MAX)];

        stringTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) textContainer:textContainer1];
        //stringTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        stringTextView.keyboardAppearance = YES;
        stringTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        stringTextView.scrollEnabled = YES;
        stringTextView.backgroundColor = [UIColor redColor];
        stringTextView.autocapitalizationType = UITextAutocapitalizationTypeWords;
        stringTextView.delegate = self;
        [self addSubview:stringTextView];
        [stringTextView becomeFirstResponder];

        NSString *sampleString=@"GPRisdoingfirst ios 7.0 project";
        textStorage = [[NSTextStorage alloc] initWithString:sampleString];
       layoutManager = [[CustomLayoutM alloc] init];//CustomLayoutM is subclass of `NSLayoutManager`

        [textStorage addLayoutManager:layoutManager];
        [textStorage addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:30.0] range:NSMakeRange(0, [textStorage length])];
       [layoutManager addTextContainer:textContainer1];

错误是:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UITextView setKeyboardAppearance:]: unrecognized selector sent to instance 0x93ab000”

首先抛出调用堆栈:

(
    0   CoreFoundation                      0x019706f4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x016f08b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01a0d983 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   libobjc.A.dylib                     0x01702959 -[NSObject forwardInvocation:] + 68
    4   CoreFoundation                      0x019607ea ___forwarding___ + 458
    5   CoreFoundation                      0x019605fe _CF_forwarding_prep_0 + 14
    6   UIKit                               0x006ff4b3 -[UITextInputTraits takeTraitsFrom:] + 1075
    7   UIKit                               0x006ffc2b +[UITextInputTraits traitsByAdoptingTraits:] + 83
    8   UIKit                               0x0063a659 -[UIKeyboardImpl takeTextInputTraitsFromDelegate] + 177
    9   UIKit                               0x0063acbb -[UIKeyboardImpl setDelegate:force:] + 1040
    10  UIKit                               0x0063a8a6 -[UIKeyboardImpl setDelegate:] + 48
    11  UIKit                               0x008e535d -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 640
    12  UIKit                               0x005c0d5a -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] + 287
    13  UIKit                               0x005c064b -[UIResponder becomeFirstResponder] + 550
    14  UIKit                               0x004c4dab -[UIView(Hierarchy) becomeFirstResponder] + 114
    15  UIKit                               0x00b33e69 -[UITextView becomeFirstResponder] + 79
    16  UIKit                               0x004c4df8 -[UIView(Hierarchy) deferredBecomeFirstResponder] + 67
    17  UIKit                               0x004c4e8c -[UIView(Hierarchy) _promoteSelfOrDescendantToFirstResponderIfNecessary] + 115
    18  UIKit                               0x004c514e __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 224
    19  Foundation                          0x014a453c -[NSISEngine withBehaviors:performModifications:] + 107
    20  Foundation                          0x01334395 -[NSISEngine withAutomaticOptimizationDisabled:] + 48
    21  UIKit                               0x004c4fcd -[UIView(Hierarchy) _postMovedFromSuperview:] + 313
    22  UIKit                               0x004d00d1 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1847
    23  UIKit                               0x004c35c1 -[UIView(Hierarchy) addSubview:] + 56
    24  PracticeiOS7                        0x000085a6 -[TextCurveView createTextViewMethod] + 518
    25  PracticeiOS7                        0x00008a9d -[TextCurveView panMethod:] + 1101
    26  UIKit                               0x007de61c _UIGestureRecognizerSendActions + 230
    27  UIKit                               0x007dd290 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
    28  UIKit                               0x007decfd -[UIGestureRecognizer _delayedUpdateGesture] + 60
    29  UIKit                               0x007e225d ___UIGestureRecognizerUpdate_block_invoke + 57
    30  UIKit                               0x007e21de _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
    31  UIKit                               0x007d88d8 _UIGestureRecognizerUpdate + 199
    32  UIKit                               0x004a6e9a -[UIWindow _sendGesturesForEvent:] + 1291
    33  UIKit                               0x004a7dba -[UIWindow sendEvent:] + 1030
    34  UIKit                               0x0047bb86 -[UIApplication sendEvent:] + 242
    35  UIKit                               0x0046635f _UIApplicationHandleEventQueue + 11421
    36  CoreFoundation                      0x018f996f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    37  CoreFoundation                      0x018f92fb __CFRunLoopDoSources0 + 235
    38  CoreFoundation                      0x019163ce __CFRunLoopRun + 910
    39  CoreFoundation                      0x01915bf3 CFRunLoopRunSpecific + 467
    40  CoreFoundation                      0x01915a0b CFRunLoopRunInMode + 123
    41  GraphicsServices                    0x030fca27 GSEventRunModal + 192
    42  GraphicsServices                    0x030fc84e GSEventRun + 104
    43  UIKit                               0x00468f0b UIApplicationMain + 1225
    44  PracticeiOS7                        0x00009b8d main + 141
    45  libdyld.dylib                       0x02dec725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
4

1 回答 1

0

您应该以正确的顺序初始化您的对象。

首先创建并附加NSTextStorage,NSTextContainerNSLayoutManager. 然后创建UITextView与准备使用NSTextContainer

// 1. Create and prepare NSTextContainer
textContainer1 = [[NSTextContainer alloc] initWithSize:CGSizeMake(2, CGFLOAT_MAX)];
NSString *sampleString=@"GPRisdoingfirst ios 7.0 project";
textStorage = [[NSTextStorage alloc] initWithString:sampleString];
layoutManager = [[CustomLayoutM alloc] init];//CustomLayoutM is subclass of `NSLayoutManager`
[textStorage addLayoutManager:layoutManager];
[textStorage addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:30.0] range:NSMakeRange(0, [textStorage length])];
[layoutManager addTextContainer:textContainer1];

// 2. Create UITextView
stringTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) textContainer:textContainer1];

// 3. setup UITextView
stringTextView.keyboardAppearance = YES;
stringTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
stringTextView.autocapitalizationType = UITextAutocapitalizationTypeWords;
// …
于 2013-09-20T20:50:12.990 回答