【问题标题】:UITextView textViewShouldBeginEditing crashes when tapped more than once多次点击时 UITextView textViewShouldBeginEditing 崩溃
【发布时间】:2012-04-17 10:12:14
【问题描述】:

我有一个 UIViewController。在这个控制器中,我以编程方式创建一个 UITextView 并将其委托设置为我的控制器。我这样做是因为我不想在点击 textView 时开始编辑它。

ViewDidLoad 方法

UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(9, 10, 302, 200)];
[textView setDelegate:self];
[self.view addSubview:textView];
[textView release];

我实现了 textViewShouldBeginEditing 方法在此处返回 NO 以禁用键盘显示。

textViewShouldBeginEditing 方法

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    NSLog(@"Shouldbegin");
    return NO;
}

出现的问题

当我点击 textView 时,它会工作一次,但如果我再次点击它,它会在没有任何日志的情况下使应用程序崩溃。当我拿着 textView 并释放它时,奇怪的是,它会像我想要的那样工作。另一方面,正常的单击第二次不起作用。

编辑

彼此快速单击似乎也可以,所以在我等待 x 秒后它似乎不起作用。

经过一些测试,我发现这似乎是一个 iOS 5.X > 错误。在 4.3 设备/模拟器中运行我的应用程序时,它会正常工作。 iOS 5.1 设备上的错误日志显示如下:

Date/Time:       2012-04-17 14:00:49.497 +0200
OS Version:      iPhone OS 5.1 (9B176)
Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000014
Crashed Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   TextInput                       0x36bf69e8 TI::Favonius::BeamSearch::choose_hit_test_node(WTF::RefPtr<TI::Favonius::SearchNode> const&, WTF::RefPtr<TI::Favonius::KeyAreaNode> const&, WTF::RefPtr<TI::Favonius::SearchNode> const&, WTF::RefPtr<TI::Favonius::SearchNode> const&) + 12
1   TextInput                       0x36bf6d1e TI::Favonius::BeamSearch::update_for_touch(unsigned int, WTF::PassRefPtr<TI::Favonius::KeyAreaNode>) + 602
2   TextInput                       0x36bfb5c2 TI::Favonius::StrokeBuildManager::update_search_for_touch(unsigned int, int) + 66
3   TextInput                       0x36bfb97c TI::Favonius::StrokeBuildManager::key_down_or_drag_hit_test_for_UI(bool, CGPoint, double, int, int, float, bool, ZT::LayoutDictionaryContext&, bool, int) + 216
4   TextInput                       0x36bddf54 TIInputManagerZephyr::simulate_touches_for_input_string() + 344
5   TextInput                       0x36bed8ba -[TIKeyboardInputManagerZephyr candidates] + 214
6   UIKit                           0x31066616 -[UIKeyboardImpl generateAutocorrectionReplacements:] + 82
7   UIKit                           0x31108a96 __71-[UITextInteractionAssistant scheduleReplacementsForRange:withOptions:]_block_invoke_0 + 370
8   UIKit                           0x3110ec62 -[UITextSelectionView calculateAndShowReplacements:] + 6
9   Foundation                      0x3762192c __NSFireDelayedPerform + 408
10  CoreFoundation                  0x361a1a2c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 8
11  CoreFoundation                  0x361a1692 __CFRunLoopDoTimer + 358
12  CoreFoundation                  0x361a0268 __CFRunLoopRun + 1200
13  CoreFoundation                  0x3612349e CFRunLoopRunSpecific + 294
14  CoreFoundation                  0x36123366 CFRunLoopRunInMode + 98
15  GraphicsServices                0x324e3432 GSEventRunModal + 130
16  UIKit                           0x30e70e76 UIApplicationMain + 1074

【问题讨论】:

  • 移除 [textView 发布];从您的 ViewDidLoad 方法中,然后尝试它。
  • 在我这边它工作正常。但我正在使用 ARC Enable。
  • 好吧,我禁用了 ARC。显示文本视图的控制器被推送到导航堆栈上,并且周围还有一些其他代码,但如果你也问我,它应该可以工作......奇怪的是它只对一小部分有用。

标签: iphone objective-c ios xcode uitextview


【解决方案1】:

我确实找到了解决方案。我真的不喜欢解决 Apple 错误,但有时你必须这样做。这是三个步骤...

1) 用不可见的视图替换默认键盘

- (void)viewDidLoad
{
    [super viewDidLoad];
    myTextView.inputView = customKeyboard;
}

2) 回答“是”以允许编辑

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return YES;
}

3) 在 textViewDidChangeSelection 中退出第一响应者以隐藏光标

- (void)textViewDidChangeSelection:(UITextView *)textView{
    [textView resignFirstResponder];
}

【讨论】:

  • 似乎可以解决:-)。我不记得我最终做了什么来修复它。
  • 我无法更改键盘类型,因为它必须是小键盘,其余步骤对我不起作用。
【解决方案2】:

如果您不想在点击时开始编辑 UITextView:

UITextView* textView = ...;
textView.editable = NO;

【讨论】:

  • 我也想知道它是否被点击:)
【解决方案3】:

经过一些测试,我发现这似乎是一个 iOS 5.X > 错误。在 4.3 设备/模拟器中运行我的应用程序时,它会正常工作。

查看我编辑日志文件的主要帖子。

【讨论】:

  • 如果你没有使用ARC,那么你应该为textView创建一个属性(retain,nonatomic),这样在viewDidLoad方法之后它就不会被释放
【解决方案4】:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{

   [txtView resignFirstResponder];

}

并在dealloc方法中释放txtView

【讨论】:

    【解决方案5】:

    斯威夫特 4.2 解决此问题的最佳且最简单的方法就是将撤消管理器设为假

    textView.undoManager?.disableUndoRegistration()
    

    如果您在运行时更改了表格中的视图,则会出现此错误,并且单元格将在点击时重新加载,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多