6

我有一个UIView包含一个UITextView. UIView是在UIViewController.

但是当我触摸UITextView盒子时,什么也没有发生。键盘不会出现,委托方法也不会响应交互。

代码:

    noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
    noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
    noteText.layer.borderWidth = 2;
    [noteText setEditable:YES];
    noteText.userInteractionEnabled = YES;
    noteText.returnKeyType = UIReturnKeyDone;
    noteText.font = [UIFont fontWithName:@"Calibri" size:16];
    noteText.textColor = [UIColor blackColor];
    [self addSubview:noteText];

更新 1

我从 UIViewController 中删除了所有其他视图,并且只在 UIViewController 中放置了一个 UITextView,但仍然没有反应。光标或键盘均未出现。

4

8 回答 8

7
noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
noteText.layer.borderWidth = 2;
[noteText setEditable:YES];
noteText.userInteractionEnabled = YES;
noteText.returnKeyType = UIReturnKeyDone;
noteText.font = [UIFont fontWithName:@"Calibri" size:16];
noteText.textColor = [UIColor blackColor];
[self addSubview:noteText];

// 两个建议

self.userInteractionEnabled = YES; // superview may blocks touches
self.clipsToBounds = YES; // superview may clips your textfield, you will see it
于 2012-10-10T08:44:00.353 回答
4

我发现了错误。

在一个类中,我对 UITextView 而不是子类进行分类,并将 canBecomeFirstResponder 设置为 NO。

于 2012-10-10T09:26:19.830 回答
3

试试这个:在你的 .h 文件中符合委托 UITextViewDelegate

:<UITextViewDelegate>

在您的 .m 文件中:

noteText.delegate = self;

和委托方法:

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

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    //resign for exapmple
    return YES;
}

希望这有帮助!

于 2012-10-09T10:28:48.740 回答
3

我知道已经晚了,但也许对某人会有所帮助。我也遇到了同样的问题,用了之后就消失了

[self setSelectable:true];

在我的 UITextView 子类中。

于 2016-11-24T09:13:22.690 回答
2

检查这些东西:

  1. 在右侧添加您的视图控制器UIWindow

  2. 使用此方法设置窗口 makeKeyAndVisible并添加rootViewController到窗口:

    -(BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    我认为 xib 文件或appdelegate.

于 2012-10-08T05:03:26.187 回答
1

尝试覆盖- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UIView 以查看它是否正在接收触摸

以防万一,尝试设置userInteractionEnabled = YES;您的 UIView 对象。如果它以某种方式设置为 NO 它将渗透到它的所有子视图

于 2012-10-04T12:50:18.060 回答
1

也许您的覆盖-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法有一些错误?也许你在resignFirstResponder那里做或者把一些观点放在首位?

于 2012-10-08T09:34:45.687 回答
1

每当发生这种情况时,请检查以下项目,

  1. 确保仅在 中正确保留和释放 Text 视图dealloc。(如果您不使用ARC
  2. 检查文本视图及其父视图的框架。确保它subview完全适合父视图。还要确保所有superviews文本视图都是这种情况。
  3. 检查是否delegate设置正确。

如果所有这些都完成,请尝试在文本视图顶部添加一个按钮并检查它target selector是否被调用。如果是,则问题出在文本视图委托或发布声明上。否则问题出在文本视图或其超级视图的框架设置上。

于 2012-10-10T08:34:31.440 回答