UITextField - 不能输入文本。键盘没有反应

Posted

技术标签:

【中文标题】UITextField - 不能输入文本。键盘没有反应【英文标题】:UITextField - can not input text. Keyboard does not responds 【发布时间】:2012-01-26 16:19:18 【问题描述】:

我有 UIViewController,里面有 UITextField。

#import <UIKit/UIKit.h>

@interface TextMemoViewController : UIViewController<UITextFieldDelegate> 

@property (unsafe_unretained, nonatomic) IBOutlet UITextField *textMemo;

@end

接下来是实现代码:

#import "TextMemoViewController.h"

@implementation TextMemoViewController
@synthesize textMemo;

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.textMemo.delegate = self;



//.....
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    return YES;

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
    [textField resignFirstResponder];
    return  YES;

问题是,当我点击 textField - 键盘出现,但什么都不能按它。所有模拟器挂断。此行为无法进行文本输入。

我有几个带有 textFields 的 UIViewControllers,一切正常。但是在这里,我找不到它发生的原因。 我已经在 Xcode 中清理了 DerivedData,重新启动了所有模拟器并为它们重置了设置。 iPhone上的情况相同。 有人有想法吗?

【问题讨论】:

您在尝试输入文本时是否检查过文本字段是否不为零?超级视野够大吗?我认为显示了超级视图之外的文本字段,但不会对输入做出反应。 不,textField 不是零。可以丰富断点在 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField return YES; textField 有它的地址和值。父视图很大。上面只有一个 textField。 【参考方案1】:

请检查您的 UIAlertView 代表。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

  // DO NOT Create Other UIAlertView.

我认为这是 ios 5.1 上的错误。

【讨论】:

【参考方案2】:

我认为你需要改变:

@property (unsafe_unretained, nonatomic) IBOutlet UITextField *textMemo;

到:

@property (strong, nonatomic) IBOutlet UITextField *textMemo;

区别是strong 而不是unsafe_unretained。我相信您的视图未被保留,这就是您无法与之交互的原因。

【讨论】:

【参考方案3】:
//make sure you use this delegate and add all your textfields..
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
UITouch* touch = [[event allTouches] anyObject];
//  NSLog(@"Touches began called.");

//fill these in with all your textfields..
if([UserFirstName isFirstResponder] && [touch view] != UserFirstName)
    [UserFirstName resignFirstResponder];



【讨论】:

【参考方案4】:

迟到总比没有好:)

谢谢大家,但正如我很久以前发现的那样,在以前的一些观点中,问题出在未辞职的响应者身上。 在这种情况下,视图被另一个视图覆盖,但响应者没有改变。

也许有人会觉得这很有用。

【讨论】:

以上是关于UITextField - 不能输入文本。键盘没有反应的主要内容,如果未能解决你的问题,请参考以下文章

UITextField 拒绝允许用户输入

如何在 UITextField 子类中拦截来自键盘的文本输入事件

UITableView 中的 UITextField 没有成为第一响应者

UITextField 键盘无法输入任何内容

获取没有文本字段/视图的键盘输入

点击另一个 UITextField 后自动禁用键盘