简体   繁体   English

在UITextView中键入bad_Access异常

[英]bad_Access exception when typing in UITextView

I don't know what is wrong with this code; 我不知道这段代码有什么问题; when i start typing in the UITextView, the program terminates with exc_Bad_Access exception. 当我开始在UITextView中键入内容时,该程序以exc_Bad_Access异常终止。

UIView *toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 430, 320, 44)];
toolbar.backgroundColor = [UIColor lightGrayColor];
UITextView *sendTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 9, 240, 26)];
sendTextView.backgroundColor = [UIColor whiteColor];
sendTextView.inputAccessoryView = toolbar;
sendTextView.layer.cornerRadius = 12.0;
[toolbar addSubview:sendTextView];
[self.view addSubview:toolbar];

The above code is inside the viewDidLoad method of a UIViewController which has a UIScrollView as its view. 上面的代码在UIViewController的viewDidLoad方法内部,该方法以UIScrollView作为其视图。

Putting an editable text view in a toolbar seems strange. 将可编辑的文本视图放在工具栏中似乎很奇怪。 (What do you do when the user wants to edit it? Move it up above the keybaord? I wouldn't expect a toolbar to move OR to contain an editable field.) Nevertheless, I'd be surprised if doing that caused EXC_BAD_ACCESS. (当用户想要编辑它时,您会怎么做?将它移到键盘上方?我不希望工具栏移动或包含可编辑字段。)但是,如果这样做会引起EXC_BAD_ACCESS,我会感到惊讶。

Your problem is more likely in code that actually runs when you're typing, such as one of the text view delegate methods. 您键入时实际运行的代码(例如,文本视图委托方法之一)中的问题更有可能出现。 If you can't find anything there, please post the stack trace at the time of the crash and code for the method that was actually running at the time. 如果在此找不到任何内容,请在崩溃时发布堆栈跟踪,并提供当时实际运行的方法的代码。

Update: After you pointed it out in your comment, I see that you're setting the toolbar as the input accessory view for sendTextView and adding it to the view controller's view. 更新:在注释中指出后,我看到您正在将工具栏设置为sendTextView的输入附件视图, 并将其添加到视图控制器的视图中。 I'd guess that what's happening here is that when you start editing the text view, the text view adds the toolbar to the keyboard's view without first removing it from the view controller's view. 我猜想这里发生的是,当您开始编辑文本视图时,文本视图将工具栏添加到键盘视图中,而没有先从视图控制器的视图中将其删除。 A given view can only be part of one view hierarchy at a time; 一个给定的视图一次只能成为一个视图层次的一部分。 adding it to your view and using it as the input accessory view won't work. 将其添加到视图并将其用作输入附件视图将不起作用。 If you look at Apple's sample code for using an accessory view you'll find that the view used as the accessory isn't part of the normal view hierarchy. 如果查看Apple使用附件视图的示例代码,您会发现用作附件的视图不属于常规视图层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM