如何以编程方式提供UITextView焦点? [英] How do I give a UITextView focus programmatically?

查看:154
本文介绍了如何以编程方式提供UITextView焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想引入一个带有UITextView的模态视图控制器,我想让键盘自动弹出,UITextView有焦点。

So I want to bring in a modal view controller that has a UITextView in it and I want the keyboard to automatically popup and the UITextView have focus.

我找到了通过执行以下操作来实现此目的:

I found a way to accomplish this by doing the following:

textView.editable = YES;
textView.editable = NO;

这对我来说似乎很难看,还有另外一种方法吗?

This just seems hacky to me, is there another way?

推荐答案

由于 UITextView 继承自 UIResponder (间接地,它实际上继承自 UIScrollView ,它继承自 UIView 哪个然后继承自 UIResponder )您可以在文本字段上调用 -becomeFirstResponder 方法,这将导致它成为第一个响应者并开始编辑:

Since UITextView inherits from UIResponder (indirectly, it actually inherits from UIScrollView, which inherits from UIView which then inherits from UIResponder) you can call the -becomeFirstResponder method on your text field, which will cause it to become the first responder and begin editing:

[textView becomeFirstResponder];

这篇关于如何以编程方式提供UITextView焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆