UITextView不会成为第一响应者

时间:2012-10-01 21:13:00

标签: objective-c ios uitextview uitextviewdelegate

我的UIView包含UITextViewUIView是在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,但仍未做出反应。光标或键盘都不会出现。

8 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

我发现了错误。

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

答案 2 :(得分: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;
}

希望这有帮助!

答案 3 :(得分:3)

我知道现在已经很晚了,但也许对某些人来说这会很有帮助。我遇到了同样的问题,使用

后它就消失了
[self setSelectable:true];

在我的UITextView子类中。

答案 4 :(得分:2)

检查一下:

  1. 在右侧UIWindow添加您的viewcontroller。

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

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

    我认为这在xib文件或appdelegate中有问题。

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

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

答案 7 :(得分:1)

每当发生类似这样的事情时,请检查以下项目,

  1. 确保仅在dealloc中正确保留和发布文本视图。(如果您未使用ARC
  2. 检查文本视图的框架及其父视图。确保subview完全适合父视图。同时确保所有superviews文本视图都是这种情况。
  3. 检查delegate是否设置正确。
  4. 如果完成所有这些操作,请尝试在文本视图顶部添加一个按钮,然后检查是否正在调用target selector。如果是,则问题在于文本视图委托或发布声明。否则问题在于文本视图的框架设置或它的超视图。

相关问题