如何防止在 UITextView 中输入文本?

发布于 2024-08-07 17:07:24 字数 422 浏览 3 评论 0原文

我的视图中有一个文本字段条目,我想在后台操作期间阻止对其进行访问。我尝试使用可编辑属性,该属性成功阻止了后台操作期间的访问,但是当我将可编辑设置为“是”时,键盘就会出现,文本字段将成为第一响应者。在更改可编辑后立即关闭键盘不会执行任何操作:

// Broken code
textView.editable = YES;
[textView resignFirstResponder];

我考虑过添加一个清晰的 UIView,它只是在关闭键盘后阻止对 UITextView 的访问,但这似乎有点矫枉过正。有正确的方法来处理这个问题吗?

这样人们就不必阅读所选答案之外的更多内容:事实证明,这是 SDK 中的“已知问题”,您可以在发行说明中找到它。使用 userInteractionEnabled 可以执行相同的功能,只要您确保自己关闭键盘即可。

I have a text field entry in my view that I would like to block access to during a background operation. I've tried using the editable property, which successfully blocks access during the background operation, but the moment I set editable to YES, the keyboard comes up and the textfield becomes the first responder. Dismissing the keyboard just after changing editable doesn't do anything:

// Broken code
textView.editable = YES;
[textView resignFirstResponder];

I've thought about adding a clear UIView that just blocks access to the UITextView after dismissing the keyboard, but that seems like overkill. Is there a correct way to handle this?

Just so people don't have to read farther than the selected answer: It turns out that this is a "known issue" in the SDK, and you can find it listed in the release notes. Using userInteractionEnabled performs the same function, as long as you make sure to dismiss the keyboard yourself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

音盲 2024-08-14 17:07:24

尝试textView.userInteractionEnabled = NO;

Try textView.userInteractionEnabled = NO;

世界等同你 2024-08-14 17:07:24

将 UIView 放在 UITextView 前面,背景颜色为深色(或白色),并且 alpha 设置较低(例如 5%),以完全覆盖 textview。默认为隐藏。

当您希望禁用文本输入时,请向其发送一个 resignFirstResponder 然后在顶部显示隐藏层。它拦截用户输入(并忽略它)。 Alpha 颜色会使其看起来“变暗”。后台操作完成后,只需将封面视图设置为隐藏即可。如果你想变得更奇特,你可以做 UIView alpha 淡入淡出动画。

Put a UIView in front of the UITextView with a dark (or white) background color and alpha set low (like 5%) sized to fully cover the textview. Default it to hidden.

When you want the textinput disabled, send it a resignFirstResponder then show the hidden layer on top. It intercepts user inputs (and ignores it). The alpha color will make it look 'dimmed.' Once your background operation is done just set the cover view to hidden and you're good to go. If you want to get fancy you can do UIView alpha fade animations.

⊕婉儿 2024-08-14 17:07:24

我不确定“正确的方法”,但我可能会做透明视图解决方案......我同意这看起来有点矫枉过正,但简单的解决方案通常是一个很好的答案。

由于视图将焦点放在更改可编辑属性上,这会更容易。

我能想到的其他解决方案是派生一个自定义UITextView并重新编码可编辑属性(或创建一个新方法)来完成您想要做的事情。这是一个很好的面向对象的解决方案,但这可能会很麻烦。

您还可以考虑使用类别来添加功能。但对于这些解决方案中的任何一个,仍然回到如何完成您所需要的事情的第一步......

I'm not sure of a "correct way" but I'd probably do the transparent view solution... I agree that it seems like overkill but the simple solution is often a good answer.

Since the view gets focus upon changing the editable properties this would be easier.

the other solution that I can think of is to derive a custom UITextView and recode the editable property (or make a new method) that can accomplish what you are trying to do. This is a good object oriented solution but this could be come cumbersome.

You might also consider using a Category to add the functionality. But for either of these solutions, are still back to square one of how to accomplish what you need...

欢你一世 2024-08-14 17:07:24

感谢上帝,有人想出了更好的回应。我最初使用以下拦截器构建它,但 userInteractionEnabled BOOL 更容易。

事实证明,该问题是 UITextView 的一个已知问题。我的解决方法:

#import <UIKit/UIKit.h>

/**
 God damn SDK bug means that you can't use editable to enable/disable a 
 UITextView (It brings up the keyboard when re-enabling)
 */
@interface StupidF_ingTextViewBlocker : UIView {
}
@end


@implementation StupidF_ingTextViewBlocker

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

@end

然后代替我上面放置的代码(而不是使用可编辑的)。禁用(假设我有一个名为 blocker 的 iVar):

// Put this in a lazy loading property or in loadView
blocker = [[StupidF_ingTextViewBlocker alloc] initWithFrame:writeView.frame];

// The blocker code  ~= textView.editable = NO
[self.view addSubview:blocker];

// Removing the blocker ~= textView.editable = YES
[blocker removeFromSuperView];

Thank god someone came up with a better response. I originally built it with the following blocker, but the userInteractionEnabled BOOL is much easier.

It turns out that the problem is a known issue with UITextView. My workaround:

#import <UIKit/UIKit.h>

/**
 God damn SDK bug means that you can't use editable to enable/disable a 
 UITextView (It brings up the keyboard when re-enabling)
 */
@interface StupidF_ingTextViewBlocker : UIView {
}
@end


@implementation StupidF_ingTextViewBlocker

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

@end

Then in place of the code I've placed above (instead of using editable). To disable (assuming I have an iVar called blocker):

// Put this in a lazy loading property or in loadView
blocker = [[StupidF_ingTextViewBlocker alloc] initWithFrame:writeView.frame];

// The blocker code  ~= textView.editable = NO
[self.view addSubview:blocker];

// Removing the blocker ~= textView.editable = YES
[blocker removeFromSuperView];
乄_柒ぐ汐 2024-08-14 17:07:24

子类 UITextView,并实现一个方法:

- (BOOL) canBecomeFirstResponder { return NO; }

Subclass UITextView, and implement a single method:

- (BOOL) canBecomeFirstResponder { return NO; }
旧夏天 2024-08-14 17:07:24

使用 UITextViewDelegate。您需要根据当前状态交换委托对象。

如果处于阻止状态,则您将使用 textViewShouldBeginEditing 返回 NO 的委托。

另一个委托的 textViewShouldBeginEditing 将返回 YES

Use the UITextViewDelegate. You'll need to swap out the delegate object depending on the current state.

If in the blocked state, then you'll use a delegate where textViewShouldBeginEditing returns NO.

the other delegate's textViewShouldBeginEditing would return YES.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文