问题描述:

I add in a UiTableViewCell a UITextField *gasPrice; like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *CellIdentifier;

if (indexPath.section == 0 && indexPath.row < [self.userCarsArray count])

CellIdentifier = [NSString stringWithFormat:@"Cell%d%d%@", indexPath.section, indexPath.row, [[self.userCarsArray objectAtIndex:indexPath.row] idCar]];

else if (indexPath.section == 0 && indexPath.row == [self.userCarsArray count])

CellIdentifier = [NSString stringWithFormat:@"Cell%d%d%@", indexPath.section, indexPath.row, @"AddCar"];

else

CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

if (indexPath.section == 1 && indexPath.row == 0) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

}

else if (indexPath.section == 2 && indexPath.row == 2) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

}

else {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

if (indexPath.section == 1 && indexPath.row == 1) {

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(170, 10, 125, 30)];

textField.adjustsFontSizeToFitWidth = YES;

textField.textColor = [UIColor colorWithRed:0.20f green:0.30f blue:0.49f alpha:1.0f];

textField.placeholder = @"0.00";

textField.keyboardType = UIKeyboardTypeDecimalPad;

textField.returnKeyType = UIReturnKeyDone;

textField.backgroundColor = [UIColor whiteColor];

textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support

textField.textAlignment = UITextAlignmentRight;

textField.tag = 0;

textField.delegate = self;

textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right

[textField setEnabled: YES];

[cell addSubview:textField];

[textField release];

gasField = textField;

}

}

// Selection style.

cell.selectionStyle = UITableViewCellSelectionStyleGray;

// Vehicles cells.

if (indexPath.section == 0) {

// ...

}

// General cells.

if (indexPath.section == 1) {

if (indexPath.row == 0) {

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.textLabel.text = @"Measurement System";

cell.textLabel.textColor = [UIColor darkGrayColor];

if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)

cell.detailTextLabel.text = @"Miles";

else

cell.detailTextLabel.text = @"Meters";

}

if (indexPath.row == 1) {

cell.textLabel.textColor = [UIColor darkGrayColor];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

if ([EcoAppAppDelegate measurement] == MeasurementTypeMile)

cell.textLabel.text = @"Gas Price (gal)";

else

cell.textLabel.text = @"Gas Price (L)";

// Gas price.

if ([EcoAppAppDelegate gasPrice] != 0.00)

gasField.text = [NSString stringWithFormat:@"%.2f", [EcoAppAppDelegate gasPrice]];

}

}

// Information cells.

if (indexPath.section == 2) {

// ...

}

return cell;

}

Then, to manage the keyboard when the user is done (because I use a keyboard without return key), I have those functions:

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

UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)] autorelease];

[self.navigationItem setLeftBarButtonItem:doneButton animated:YES];

return YES;

}

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

[self.navigationItem setLeftBarButtonItem:nil animated:YES];

// Save gas price.

if ([textField.text isEqualToString:@""])

[EcoAppAppDelegate setGasPrice:0.00];

else

[EcoAppAppDelegate setGasPrice:[textField.text floatValue]];

return YES;

}

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

// Hide keyboard.

[textField resignFirstResponder];

return YES;

}

And the function for the navigation bar button "Done":

- (void)done:(id)sender {

if ([gasField canResignFirstResponder]) {

[gasField resignFirstResponder];

}

[self.navigationItem setLeftBarButtonItem:nil animated:YES];

}

So it works fine first, when I stay in my settings view controller, but when I do other stuff on some other cells (that push other view controllers, come back, etc), it stops working. It is probably because the gasField pointer has disappeared or something like that, the gasField object tho is still different from nil.

Any idea why it does that? Thanks!

网友答案:

I think you are using gasField as a property. use

self.gasField
相关阅读:
Top