textFieldShouldBeginEditing没有响应

时间:2013-11-17 09:56:14

标签: ios objective-c uitextfield

#import <UIKit/UIKit.h>
#import "HZAreaPickerView.h"

@interface CodeNumberViewController : UIViewController<UITextFieldDelegate, HZAreaPickerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *areaText;
@property (weak, nonatomic) IBOutlet UITextField *cityText;


@property (strong, nonatomic) NSString *areaValue, *cityValue;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;

-(void)cancelLocatePicker;
@end

-----------------下面的.m文件--------------

#pragma mark - TextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:self.areaText]) {
        [self cancelLocatePicker];
        self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
        [self.locatePicker showInView:self.view];
    } else {
        [self cancelLocatePicker];
        self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCity delegate:self];
        [self.locatePicker showInView:self.view];
    }
    return NO;
}

当我单击UITextField时,我发现textFieldShouldBeginEditing甚至没有被调用。怎么了?我已经将UITextField连接到.h文件。

我真的希望你能提供帮助,如果你需要更多细节,我会补充一点,不要因为我没有太多而减去我的声誉。感谢

3 个答案:

答案 0 :(得分:2)

我猜你没有在viewDidLoad集中设置委托:

self.areaText.delegate = self;

self.cityText.delegate = self;

答案 1 :(得分:0)

您是否在代码中连接了UITextField委托或提及?

答案 2 :(得分:0)

首先,您必须像已经完成的那样添加UITexFieldDelegate,但是在初始化时还需要将UITextField与委托连接起来。您可以以编程方式添加它,也可以只在xib文件(或故事板)中连接它。

编程:

 areaText; = [[UILabel alloc] initWithFrame:frane];
 areaText.delegate = self;
[self.view addSubview:areaText];