iphone - 如何添加清除按钮 UITextfield?

标签 iphone ios button uitableview uitextfield

<分区>

我想在 UITextfield 中添加一个清除按钮,但它没有显示。

这是 UITextfield 的代码:

  @implementation databaseEnterDataViewController

@synthesize customer = customer_ ;
@synthesize type = type_ ;
@synthesize code1 = code1_ ;
@synthesize code2 = code2_ ;
@synthesize background,
            changeType,
            codeOne,
            codeTwo,
            customers,
            suspendDisplayInfo,
            tf;


#pragma mark -
#pragma mark Initialization

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    self.customer = @"" ;
    self.type     = @"" ;
    self.code1    = @"" ;
    self.code2    = @"" ;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonSystemItemCancel target:self action:nil];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil];
    self.navigationItem.title = @"Enter Data";

    code1Field_.clearButtonMode = UITextFieldViewModeWhileEditing;
    code2Field_.clearButtonMode = UITextFieldViewModeUnlessEditing;
    customerField_.clearButtonMode = UITextFieldViewModeAlways;


    [self openDB];
    [self createTable:@"barcodeScan" withField1:@"key" withField2:@"scanDate" withField3:@"theCustomer" withField4:@"type" withField5:@"theCode1" withField6:@"theCode2" withField7:@"discription" withField8:@"articleNr" withField9:@"batchNr" withField10:@"serialNr" withField11:@"expDate"];

    self.suspendDisplayInfo=false;
    NSLog(@"%@",typeLabel_.text);









#ifdef LOG_FILE
    NSFileManager *fileManger = [NSFileManager defaultManager];
    if ([fileManger fileExistsAtPath:[self getLogFile]])
    {
        [debug appendString:[[NSString alloc] initWithContentsOfFile:[self getLogFile]]];
        [debugText setText:debug];
    }
#endif

    dtdev=[Linea sharedDevice];
    [dtdev addDelegate:self];
    [dtdev connect];




    types = [[NSMutableArray alloc] init];
    [types addObject:@"Select barcode type"];
    [types addObject:@"Intervascular"];
    [types addObject:@"CID"];
    [types addObject:@"MAQUET"];


    [self pickerview];

    tf.clearButtonMode =UITextFieldViewModeAlways;


}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 4;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // Make cell unselectable
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

     tf = nil;
    tf.clearButtonMode =UITextFieldViewModeAlways;
    changeType = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    changeType.frame = CGRectMake(cell.frame.origin.x + 220, cell.frame.origin.y + 7, 80, 30);
    [changeType setTitle:@"Change type" forState:UIControlStateNormal];
    changeType.backgroundColor= [UIColor clearColor];
   [changeType.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [changeType addTarget:self action:@selector(clickeButton:) forControlEvents:UIControlEventTouchDown];


    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Customer:" ;
            tf = customerField_ = [self makeTextField:self.customer placeholder:@"Customer name"];
            [cell addSubview:customerField_];


            break ;
        }
        case 1: {
            cell.textLabel.text = @"Type:" ;
            tf = typeLabel_ = [self makeTextField:self.type placeholder:@"Type code"];
            [cell addSubview:typeLabel_];
            [cell addSubview:changeType];

            break ;
        }
        case 2: {
            cell.textLabel.text = @"Code 1:" ;
            tf = code1Field_ = [self makeTextField:self.code1 placeholder:@"Code 1"];
            [cell addSubview:code1Field_];
            break ;
        }
        case 3: {
            cell.textLabel.text = @"Code 2:" ;
            tf = code2Field_ = [self makeTextField:self.code2 placeholder:@"code 2"];
            [cell addSubview:code2Field_];
            break ;
        }
    }

    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];

    // We want to handle textFieldDidEndEditing
    tf.delegate = self ;

    return cell;
}





-(UITextField*) makeTextField: (NSString*)text
                  placeholder: (NSString*)placeholder  {
    UITextField*tf = [[UITextField alloc] init];
    tf.placeholder = placeholder ;
    tf.text = text ;
    tf.autocorrectionType = UITextAutocorrectionTypeNo ;
    tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tf.adjustsFontSizeToFitWidth = YES;
    tf.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
    return tf ;
}

// Workaround to hide keyboard when Done is tapped
- (IBAction)textFieldFinished:(id)sender {
    // [sender resignFirstResponder];
}

// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ( textField == customerField_ ) {
        self.customer = textField.text ;
    }
    else if ( textField == typeLabel_ ) {
        self.type = textField.text ;
    }
    else if ( textField == code1Field_ ) {
        self.code1 = textField.text ;
    }
    else if ( textField == code2Field_ ) {
        self.code2 = textField.text ;
    }
}

在 iphone 上它看起来像这样:

enter image description here

当我像这样添加清除按钮时

customerField_.clearButtonMode = UITextFieldViewModeWhileEditing;

按钮没有显示。

我应该怎么做才能让它可见?

最佳答案

如果您使用 UITextFieldViewModeAlways,如果文本字段的字符串不为 nil 或者它的长度 > 0,您将看到该按钮。我认为您不能显示 clearbutton 如果没有什么要清除的。

关于iphone - 如何添加清除按钮 UITextfield?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16495872/

相关文章:

html - 网页高度在移动浏览器中被拉伸(stretch)

iphone - 在 CoreData 上保存和检索 UIImage

iphone - 围绕 UITableView 的一部分的阴影

ios - Cocoa Touch 获取用户全名

ios - 使用自动布局时设置 UICollectionViewFlowLayout 的属性不起作用

iphone - iOS:对正确使用 AppDelegate 感到困惑

ios - 如何在 ios 中发送带有链接和图像的自定义推文?

jquery - 如何让CSS按钮显示透明

javascript - Internet Explorer 9 显示按钮不同于 IE 10、Firefox 和 Safari 等

jQuery 用值替换输入元素