UITextField clearButtonMode

时间:2012-11-12 06:20:54

标签: ios uitextfield

我将UITextField的clearButtonMode设置为UITextFieldViewModeWhileEditing,用于我以编程方式创建的三个Textfield。它在前两个文本字段上运行良好。但不是在最后一个领域工作。单击清除按钮而不是清除文本时隐藏键盘。 任何人都可以建议我在这里做错了什么? 这是我的代码:

    UILabel *caption1 = [[UILabel alloc ]initWithFrame:CGRectMake(0,0,320.0, 50)];
    caption1.textAlignment =  UITextAlignmentCenter;
    caption1.textColor = [UIColor whiteColor];
    caption1.backgroundColor = [UIColor clearColor];
    caption1.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(14.0)];
    caption1.text = @"By Specialty";
    caption1.center=CGPointMake(160.0, 150.0);
    [self addSubview:caption1];


    SpecialtyBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [SpecialtyBtn addTarget:myController action:@selector(specialtyBtnPress) forControlEvents:UIControlEventTouchUpInside];
    SpecialtyBtn.frame = CGRectMake(0, 0, 300, 40);
    [SpecialtyBtn setTitle:@"(none)" forState:UIControlStateNormal];
    SpecialtyBtn.center=CGPointMake(160.0,185.0);
    [self addSubview:SpecialtyBtn];



    UILabel *caption2 = [[UILabel alloc ]initWithFrame:CGRectMake(0,0,320.0, 50)];
    caption2.textAlignment =  UITextAlignmentCenter;
    caption2.textColor = [UIColor whiteColor];
    caption2.backgroundColor = [UIColor clearColor];
    caption2.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(14.0)];
    caption2.text = @"Select Location (e.g. NN16 8XF)";
    caption2.center=CGPointMake(160.0, 240.0);
    [self addSubview:caption2];


    locationTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, 300, 40)];
    locationTF.placeholder = @"(Current Location)";
    locationTF.textAlignment = UITextAlignmentCenter;
    locationTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    locationTF.backgroundColor=[UIColor whiteColor];
    locationTF.layer.cornerRadius = 10;
    locationTF.clipsToBounds = YES;
    locationTF.delegate = self;
    locationTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    locationTF.center=CGPointMake(160.0,275.0);
    [self addSubview:locationTF];


    UILabel *caption3 = [[UILabel alloc ]initWithFrame:CGRectMake(0,0,320.0, 50)];
    caption3.textAlignment =  UITextAlignmentCenter;
    caption3.textColor = [UIColor whiteColor];
    caption3.backgroundColor = [UIColor clearColor];
    caption3.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(14.0)];
    caption3.text = @"Advanced Search (Consultant Name)";
    caption3.center=CGPointMake(160.0, 325.0);
    [self addSubview:caption3];


    interestsTF = [[UITextField alloc] initWithFrame:CGRectMake(0, 300, 300, 40)];
    interestsTF.placeholder = @"(none)";
    interestsTF.textAlignment = UITextAlignmentCenter;
    interestsTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    interestsTF.backgroundColor=[UIColor whiteColor];
    interestsTF.layer.cornerRadius = 10;
    interestsTF.clipsToBounds = YES;
    interestsTF.delegate = self;
    interestsTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    interestsTF.center=CGPointMake(160.0,360.0);
    [self addSubview:interestsTF];


    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* btnImg=[UIImage imageNamed:@"searchBtn.png"];
    [searchBtn setBackgroundImage:btnImg forState:UIControlStateNormal];
    [searchBtn addTarget:myController action:@selector(searchBtnPress) forControlEvents:UIControlEventTouchUpInside];
    searchBtn.frame = CGRectMake(0, 0, btnImg.size.width/2,btnImg.size.height/2);
    searchBtn.center=CGPointMake(160.0,420.0);

    [self addSubview:searchBtn];

0 个答案:

没有答案