views:

95

answers:

2

I currently have two UITextFields inside two corresponding UITableViewCells.

This is how it looks:

- (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] autorelease];
}

// Configure the cell.

//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.

if ([indexPath section] == 0) {
    tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
    tUser.adjustsFontSizeToFitWidth = YES;
    tUser.textColor = [UIColor blackColor];

    tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
    tPass.adjustsFontSizeToFitWidth = YES;
    tPass.textColor = [UIColor blackColor];

    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            tUser.placeholder = @"@JohnAppleseed";
            tUser.keyboardType = UIKeyboardTypeEmailAddress;
            tUser.returnKeyType = UIReturnKeyNext;
        }
        if ([indexPath row] == 1) {
            tPass.placeholder = @"Required";
            tPass.keyboardType = UIKeyboardTypeDefault;
            tPass.returnKeyType = UIReturnKeyDone;
            tPass.secureTextEntry = YES;
        }
    }

    tUser.backgroundColor = [UIColor whiteColor];
    tUser.autocorrectionType = UITextAutocorrectionTypeNo;
    tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tUser.textAlignment = UITextAlignmentLeft;

    tPass.backgroundColor = [UIColor whiteColor];
    tPass.autocorrectionType = UITextAutocorrectionTypeNo;
    tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
    tPass.textAlignment = UITextAlignmentLeft;

    tUser.clearButtonMode = UITextFieldViewModeNever;
    tPass.clearButtonMode = UITextFieldViewModeNever;

    [tUser setEnabled:YES];
    [tPass setEnabled:YES];

    //[tUser release];
    //[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
    if ([indexPath row] == 0) { // Email
        cell.textLabel.text = @"Username";
        [cell addSubview:tUser];
        [tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_name_preference"]];
    }
    else {
        cell.textLabel.text = @"Password";
        [cell addSubview:tPass];
        [tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:@"twitter_pass_preference"]];
    }
}
return cell; }

As you can see, these work and when I load the UITableView the UITextFields load into the correct UITableViewCells. However, as you can see I pull two NSUserDefault objects, both placed into the corresponding UITextFields.

However, I then have another action attached to a save button which is displayed in the Navigation Bar.

-(void)save_clicked: (id) sender {

if ([tPass text] == nil) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                    message:@"There was no password entered, please enter the correct password and try again." 
                                                   delegate:self 
                                          cancelButtonTitle:@"Okay" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

else {
    NSLog(@"we can do something here soon...");

    //NSString *tUserString = [[NSString alloc] initWithFormat:@"Hello: %@", tUser.text];

    NSLog(@"We saved their username: %@", [tUser text]);
    NSLog(@"We saved their password: %@", [tPass text]);

    // here we will start saving the username and password, then obtaining the authentication shit.



}
}

However, when I call these NSLogs, tUser returns (null) but tPass returns the entered text. I am sure it is just a simple syntax error or something of the sort, as everything (from my point of view) looks like it should work. Albeit, it doesn't.

Can someone aid me in figuring out what is wrong with the tUser UITextField and as to why it keeps returning (null)?

All help is appreciated!

A: 

You're adding new tUser and tPass text fields every time your cells get displayed. You should keep your cell creation code (adding of text fields) inside the if (cell == nil) block and configure those text fields outside of that block. That way, you won't add new text fields every time tableView:cellForRowAtIndexPath: is called.

James Huddleston
You able to elaborate by adding a code snippet?
the0rkus
Don't worry about it, got it working! :D
the0rkus
A: 

theOrkus, how did you fix it?

pettaiphone