2011-10-12 26 views
8

我试图使用textFieldShouldBeginEditing来禁用键盘显示自定义UITextField。我正在实施所有UITextFieldDelegate方法。但是,由于某种原因,textFieldShouldBeginEditing实际上永远不会被调用。textFieldShouldBeginEditing not called

下面的委托方法总是叫:

– textFieldDidBeginEditing: 
– textFieldShouldEndEditing: 
– textFieldDidEndEditing: 

该视图以下列方式结构:

UIViewController,其保持滚动视图。根据视图的状态,此滚动视图将包含UIView以及自定义UITextFields的列表。

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

任何想法?

编辑;添加了一些代码段:

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 
+0

如果您的其他委托方法正在针对相同的文本字段进行调用,并且您的文本字段不是子类或任何内容,则只能来自方法名称中的拼写错误。 – jrturton

+0

我已经对UITextField进行了子类化,以对文本字段的外观进行一些修改。但是,我实际上没有触及与代表有关的任何事情。 – Charles

+0

我不确定这个,但是,是否可以通过方法“canBecomeFirstResponder”的默认实现调用“textFieldShouldBeginEditing”?尝试通过[super canBecomeFirstResponder]实现该方法或仅​​仅删除它。 – lluismontero

回答

13

它是更多钞票那textFieldShouldBeginEditing是由方法canBecomeFirstResponder的默认实现调用的?

尝试执行[super canBecomeFirstResponder]的方法或只是删除它。

+0

谢谢!我有一个'UITextField'子类,由于一些经验性测试而覆盖'canBecomeFirstResponder'。完全错过了 – mkko

13

有你设置的UITextField授人以 “自我”?

+0

UITextField委托设置为self。但是,这是从主UIViewController调用的。委托方法的实现也在主UIViewController中。 – Charles

+0

你必须设置UITextView委托给实现UITextViewDelegate方法的类 – lluismontero

+0

是的,这就是我所做的。 :) – Charles

0

不知道是否是一个错字错误在这里复制的代码,但该方法名是不正确的:

正确 - textFieldShouldBeginEditing

YOURS - textFieldShoulBeginEditing(失踪 “d”)

+0

复制错误。 ( – Charles

+2

,这将只会导致编译错误... –

2

对于任何人来到这里,并没有找到解决办法。我的问题是我在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]; 
2

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

当UITextFields位于ViewController子类的一部分时,委托方法被调用。

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

在ViewDidLoad中将其应用程序委托设置为self可解决此问题。

self.emailField.delegate = self; 
self.passwordField.delegate = self; 
+0

令人惊叹。在我的情况下,我有一种观点在导航控制器中,没有任何工作正确......触摸事件,文本框等。我按照你的建议分配了代表,并且它工作......完全废话。 –

0

在Swift-3中需要在textField之前的“_”以便这个委托方法将会调用。在我的情况下,它可以帮助我。

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