2013-05-11 117 views
2

我想在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;

該按鈕沒有出現。

我該怎麼做才能看到它?

+2

它被稱爲'UITextFieldViewModeWhileEditing',因爲該按鈕僅在編輯時可見。編輯意味着您的領域是第一響應者,並且鍵盤已經啓動。嘗試'UITextFieldViewModeAlways' – 2013-05-11 09:58:34

+0

它不起作用 – Thymen 2013-05-11 10:05:36

回答

6

如果你使用UITextFieldViewModeAlways,你會看到按鈕,如果文本字段的字符串不是零,或者它的長度大於0.我認爲如果沒有任何可以清除的東西,你不能顯示清除按鈕。

+0

當我在其中放入文本時,它不起作用 – Thymen 2013-05-11 10:15:34

+1

在cellForRowAtIndexPath中,您將clearButton設置爲零對象,請嘗試在文本字段初始化之後的情況0中將其設置在開關中。 – Mat 2013-05-11 10:26:08

+0

謝謝你的幫助 – Thymen 2013-05-11 10:32:12

3

如果您希望清除按鈕始終可見,那麼您需要將文本字段的clearButtonMode屬性設置爲UITextFieldViewModeAlways。您當前配置文本字段的方式,只有在編輯文本字段時,清除按鈕纔會顯示。

編輯:正如Mat在註釋中指出的那樣,無論文本字段的clearButtonMode屬性是什麼值,清除按鈕僅在相關時才顯示,即如果有要清除的內容。如果文本字段爲空,則不需要顯示清除按鈕。

+0

我試過,但它不起作用 – Thymen 2013-05-11 10:03:46

+3

無論如何,如果字符串長度爲0,您將無法看到清除按鈕。 – Mat 2013-05-11 10:03:57

+0

@Mat這是真的。我編輯了我的答案,以包含這個小而關鍵的細節。 – 2013-05-11 10:11:08

相關問題