【发布时间】: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