TextField中的标记不起作用

时间:2014-12-26 08:24:31

标签: objective-c xcode uitextfielddelegate

 #import <UIKit/UIKit.h>

@interface CustomTF : UITextField

-(void)awakeFromNib;

@end

and



#import "CustomTF.h"

@implementation CustomTF

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)awakeFromNib
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {  // for iphone ....

        self.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1];
        //        self.font = [UIFont fontWithName:@"HelveticaNeueLT-LightCond" size:15];

        self.borderStyle = UITextBorderStyleRoundedRect;
        self.font = [UIFont systemFontOfSize:15];
//        self.placeholder = @"enter text";
        self.autocorrectionType = UITextAutocorrectionTypeNo;
        self.keyboardType = UIKeyboardTypeDefault;
        self.returnKeyType = UIReturnKeyDefault;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;



        self.font = [UIFont fontWithName:@"Helvetica" size:15];

    }
    else
    {   // for ipad .....
        self.textColor = [UIColor colorWithRed:51/255.0f green:51/255.0f blue:51/255.0f alpha:1];
        //        self.font = [UIFont fontWithName:@"Anivers-Regular" size:17];

        self.font = [UIFont fontWithName:@"Helvetica" size:17];

    }

//    self.layer.borderColor = [UIColor blackColor].CGColor;
//    self.layer.borderWidth = 1.0f;

}




@end

//根据运行时创建setTimeTF numberOfTime = 5;

 for (int i=1; i<= numberOfTime; i++) {
        setTimeLabel = [[CustomLBL alloc] initWithFrame:CGRectMake(lblX, lblY, lblW, lblH)];
        setTimeTF = [[CustomTF alloc] initWithFrame:CGRectMake(tfX, tfY, tfW, tfH)];
        setTimeTF.placeholder = @"HH:MM";

        lblY = lblY + lblH + 10;
        tfY = tfY + tfH + 10;




        setTimeTF.borderStyle = UITextBorderStyleRoundedRect;
        setTimeTF.font = [UIFont systemFontOfSize:15];
        setTimeTF.autocorrectionType = UITextAutocorrectionTypeNo;
        setTimeTF.keyboardType = UIKeyboardTypeDefault;
        setTimeTF.returnKeyType = UIReturnKeyDone;
        setTimeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        setTimeTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        setTimeTF.delegate = self;
        setTimeTF.backgroundColor=[UIColor clearColor];
        if (i == 1) {
            setTimeTF.tag = 11;
            setTimeLabel.text = @"Set 1st Time";
        }
        else if (i == 2){
            //setTimeTF.tag = 22;
            setTimeLabel.text = @"Set 2nd Time";
        }
        else if (i == 3){
            setTimeTF.tag = 33;
            setTimeLabel.text = @"Set 3rd Time";
        }
        else if (i == 4){
            setTimeTF.tag = 44;
            setTimeLabel.text = @"Set 4th Time";
        }
        else if (i == 5){
           setTimeTF.tag = 55;
            setTimeLabel.text = @"Set 5th Time";
        }





        [setTimeVw addSubview:setTimeLabel];
        [setTimeVw addSubview:setTimeTF];


    }

我的问题:

在textFieldShouldBeginEditing的委托方法中,它不会出现条件

if(textField == self.setTimeTF)并且标记设置始终为55(最后一个标记)

#pragma mark - TextField Delegates

- (BOOL) textFieldShouldBeginEditing: (UITextField *) textField
{

  if (textField == self.setTimeTF)
    {
      if (setTimeTF.tag == 11)
        {
            [self showTimePicker:setTimeTF.tag];
            return NO;

        }
        else if (setTimeTF.tag == 22)
        {

            [self showTimePicker];
            return NO;

        }
        else if (setTimeTF.tag == 33)
        {

            [self showTimePicker];
            return NO;

        }

        else if (setTimeTF.tag == 44)
        {

            [self showTimePicker];
            return NO;

        }
        else if (setTimeTF.tag == 55)
        {

             [self showTimePicker];
            return NO;

        }

        }



    return YES;

    //    return NO;
}

0 个答案:

没有答案