IOS中的Textfield问题

时间:2017-12-07 09:16:58

标签: ios objective-c

我在视图控制器下的文本字段中有一个文本字段我使用了一个表格视图来显示其中的数据,当我们点击表格视图中的任何数据时,它显示在文本字段中。现在,当我按下表格视图的任何单元格以显示文本字段中的数据时它显示正常,但是文本字段不会从表格视图中捕获数据,它只会发送手动写入文本字段的数据。它在控制台中显示了这个,

<UITableViewCell: 0x7f9dad0a1000; frame = (0 132; 342 44); text = '2014'; autoresize = W; layer = <CALayer: 0x60400022ec20>>

我的代码就是这个,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView==_year01) {
    return [year1Array count];
}else if (tableView == _carTable){
    return [carArray count];
}
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == _year01){
    cell.textLabel.text=[year1Array objectAtIndex:indexPath.row];
}else if (tableView == _carTable){
    cell.textLabel.text=[carArray objectAtIndex:indexPath.row];
}

cell.backgroundColor=[UIColor darkGrayColor];
cell.textLabel.textColor=[UIColor whiteColor];


 return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Cell %@",selectedCell);
if (tableView == _year01){
    self.year1.text=[year1Array objectAtIndex:indexPath.row];
    self.year01.hidden=YES;
}else{
    self.carName.text=[carArray objectAtIndex:indexPath.row];
    _carTable.hidden=YES;
    [_carName resignFirstResponder];
}
}

管理textfield中的空格的代码就是这个,

-(void)textFieldDidChanges :(UITextField *)theTextField{
result=self.carName.text;
result=[result stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSLog(@"RRR %@",result);
//just use this result where you want
}

将数据发送到下一个视图

- (IBAction)okay:(id)sender {

if ([_carName.text isEqualToString:@""] || [_year1.text isEqualToString:@""] || [_year2.text isEqualToString:@""] ) {

    NSString *message = @"Fill all the fields";

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Enter Data"
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:YES completion:nil];

    int duration = 2; // duration in seconds

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });


    _guideView.hidden=YES;
}else{

NSString *strAppend = [NSString stringWithFormat:@"%@-%@",_year1.text,_year2.text];
NSLog(@"%@",strAppend);

[[NSUserDefaults standardUserDefaults] setObject:result forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setObject:strAppend forKey:@"year"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

}

1 个答案:

答案 0 :(得分:0)

在didSelectRow中分配新值之前清空文本字段。

if (tableView == _year01)
{
   self.year1.text=@"";

   self.year1.text=[year1Array objectAtIndex:indexPath.row];
   self.year01.hidden=YES;
}
else
{
   self.carName.text=@"";

   self.carName.text=[carArray objectAtIndex:indexPath.row];
   _carTable.hidden=YES;
  [_carName resignFirstResponder];

}