【问题标题】:textFieldShouldBeginEditing not being calledtextFieldShouldBeginEditing 未被调用
【发布时间】:2011-10-12 16:55:58
【问题描述】:

我正在尝试使用textFieldShouldBeginEditing 来禁用键盘以显示自定义UITextField。我正在实现所有UITextFieldDelegate 方法。然而,出于某种原因,textFieldShouldBeginEditing 实际上从未被调用过。

以下委托方法总是被调用:

– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:

视图的结构如下:

UIViewController 包含一个滚动视图。根据视图的状态,此 ScrollView 将包含一个 UIView 和一个自定义 UITextFields 列表。

我在此设备上运行 iOS 4.3.5 (8L1)。

有什么想法吗?

编辑;添加了一些代码sn-ps:

UIViewController有如下接口

@interface AViewController: UIViewController<UITextFieldDelegate>

一旦 UIViewController 加载,我将所有 UITextFields 连接到视图使用

aSubView.aTextField.delegate = self;

(简化)位于 AViewController 中的委托实现

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}

自定义UITextField代码

简化的实现文件--

#import "PVEntryTextField.h"
#import "EntryViewController.h"

@implementation PVEntryTextField
@synthesize isPasswordField, state, singleTap;

- (id)initWithCoder:(NSCoder *)inCoder
{
    if (self = [super initWithCoder:inCoder])
    {
         self.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
         self.textColor = [UIColor colorWithRed:51.0/255.0 
                                         green:51.0/255.0 
                                          blue:51.0/255.0 
                                         alpha:1.0];
         self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }

    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.origin.x + 16, bounds.origin.y,
                      bounds.size.width - 16*2 - 10, bounds.size.height);
}

- (CGRect) editingRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) updateState:(int) newState
{
     state = newState;
 }

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

 @end

【问题讨论】:

  • 如果您的其他委托方法正在为同一个文本字段调用,并且您的文本字段不是子类或任何东西,这只能来自您的方法名称中的拼写错误。
  • 我对 UITextField 进行了子类化,以对文本字段的外观进行一些修改。但是,我实际上并没有触及任何与代表有关的东西。
  • 我对此不确定,但是,是否有可能通过方法“canBecomeFirstResponder”的默认实现调用“textFieldShouldBeginEditing”?尝试通过 [super canBecomeFirstResponder] 实现该方法或直接删除它。
  • 你是对的。它现在正在工作!谢谢!写这个作为回复,我会接受的。 :)

标签: iphone objective-c ios mobile


【解决方案1】:

textFieldShouldBeginEditing 是否有可能被方法canBecomeFirstResponder 的默认实现调用?

尝试通过[super canBecomeFirstResponder] 实现该方法或直接删除它。

【讨论】:

  • 谢谢!由于一些经验测试,我有一个UITextField 子类覆盖canBecomeFirstResponder。完全错过了。
  • Iluismontero 是绝对正确的!似乎实现“canBecomeFirstResponder”方法的子类应该在逻辑中的某个点调用 super。我在 UITextField 子类中遇到了完全相同的问题,并将 [super canBecomeFirstResponder] 放入我的代码中解决了这个问题。
【解决方案2】:

您是否将 UITextField 委托设置为“self”?

【讨论】:

  • UITextField 委托设置为 self。但是,这是从主 UIViewController 调用的。委托方法的实现也在主 UIViewController 中。
  • 您必须将 UITextView 委托设置为实现 UITextViewDelegate 方法的类
【解决方案3】:

对于来到这里但没有找到解决方案的任何人。我的问题是我在 IB 中创建了 textField,然后在我的 viewDidLoad 中分配了一个。当我删除实例化时,委托正常工作,因为它绑定到正确的 TF。

//I REMOVED these two lines because I created the textfield in IB
_nameTextField = [[UITextField alloc] init];
_priceTextField = [[UITextField alloc] init];


[_nameTextField setDelegate:self];
[_priceTextField setDelegate:self];

【讨论】:

    【解决方案4】:

    我还遇到了没有调用 textFieldShouldEndEditing 或 textFieldShouldReturn 的问题。这发生在更新我的应用的 Storyboard 之后。

    当 UITextFields 是 ViewController 子类的一部分时,会调用委托方法。

    但是,当它们被移动到 ViewController 中的滚动视图中时,不再调用这些方法。

    在 ViewDidLoad 中将他们的应用代理设置为 self 解决了这个问题。

    self.emailField.delegate = self;
    self.passwordField.delegate = self;
    

    【讨论】:

    • 太棒了。在我的情况下,我有一个显示在导航控制器中的视图,但没有任何工作正常......触摸事件,文本框等。我按照你的建议分配了代表,它工作......完全废话。
    【解决方案5】:

    不知道这里复制代码是不是拼写错误但方法名不正确:

    正确 - textFieldShouldBeginEditing

    您的 - textFieldShoulBeginEditing(缺少“d”)

    【讨论】:

      【解决方案6】:

      在 Swift-3 中,以下方法需要在 textField 之前添加“_”,以便调用此委托方法。就我而言,它对我有帮助。

      func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多