0

I made an outlet for a textfield in storyboard. The text field is in a TableViewCell, so I set this up in UsernameTableViewCell:

@property (strong, nonatomic) IBOutlet UITextField *username;

Then, in the SignUpTableViewController, I set am trying to retrieve the value of the TextField that the user enters here:

UsernameTableViewCell *usernameObject = [[UsernameTableViewCell alloc]init];

Now, when I try to get the value of the textfield, I get (null):

NSLog(@"%@", usernameObject.username.text);

How can I fix this so that usernameObject.username.text displays the value that the user entered?

Update:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        UsernameTableViewCell *usernameObject = [[UsernameTableViewCell alloc]init];
        PasswordTableViewCell *passwordObject = [[PasswordTableViewCell alloc]init];
        NSLog(@"%@", usernameObject.username.text);
        PFUser *user = [PFUser user];
        user.username = usernameObject.username.text;
        user.password = passwordObject.password.text;
        user.email = @"email@example.com";

        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
                // Hooray! Let them use the app now.
            } else {
                NSString *errorString = [error userInfo][@"error"];
                // Show the errorString somewhere and let the user try again.
            }
        }];
    }
}

2 Answers2

1

First of all you should be dequeuing your cells.

Tag a reuse identifier for you UITableViewCell in the storyboard. Then in your tableviewdatadelegate, you should be using the following code to create the cell.

    UsernameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usernameCell" forIndexPath:indexPath];

UsernameTableViewCell should have a reference to the UITextField you created as an IBOutlet.

Now when the user clicked on a cell, i am assuming you are letting the user continue to type?

If you are using some other click to detect that the user is done typing then, you will have to write your own function to first get the cell at that index and then look up the UITextField in that cell and then access the text in that.

If you provide more code and details about what you are doign with the cells, it wil be easier to help

hackerinheels
  • 1,141
  • 1
  • 6
  • 14
  • I don't know what you mean by that. After I put that line in didSelectRowAtIndexPath, I type UsernameTableViewCell.UITextField.username and it says UITextField is not found under UsernameTableViewCell. – user3398659 Jul 16 '14 at 23:18
  • UsernameTableViewCell and UITextField are class names - its like saying NSArray.int.varName. You should post your code – hackerinheels Jul 16 '14 at 23:22
  • you are creating new cells when user clicked on the cell and expecting them to return correct data. This line: UsernameTableViewCell *usernameObject = [[UsernameTableViewCell alloc]init]; is createing a brand new instance of UsernameTableViewCell with empty contents. You are trying to read them. where are you having user add content to the cells? – hackerinheels Jul 16 '14 at 23:39
  • I made a textfield inside a tableviewcell. I'm just trying to get what the user types into that into the tableviewcontroller. – user3398659 Jul 16 '14 at 23:43
  • read this link http://stackoverflow.com/questions/4375442/accessing-uitextfield-in-a-custom-uitableviewcell – hackerinheels Jul 17 '14 at 00:36
  • I don't understand that. Can you help me figure out a simple way of what to do? – user3398659 Jul 17 '14 at 02:03
0

//step 1: make muteable array last deal

        @implementation settingViewController{
         int flag;
        NSMutableArray *lastDeal;

        }

//step 2: initialize lastdeal in view did load

    - (void)viewDidLoad {
     lastDeal=[[NSMutableArray alloc]init];
    }

//step 3: create cell with your class and give tags to cell and assign delegates

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

        simpleCellIdentifier=@"settingTableItem";
            UsernameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            cell = [[UsernameTableViewCell alloc]      initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
        [cell.textField setTag:indexPath.row];
           [cell.itemField setDelegate:self];
          }

// step 4 : just copy the below code it will put your user name at 0 index of lastDeal and at 1 index it will put your password.

now you can use array info at anywhere.

      -(void)textFieldDidEndEditing:(UITextField *)textField{
        [self.lastDeal removeObjectAtIndex:textField.tag];
          [self.lastDeal insertObject:textField.text atIndex:textField.tag];
       }
Muhammad Sarim
  • 401
  • 5
  • 11