Sam Parrish
Sam Parrish

Reputation: 1387

get UITextField text from UITableView

I have created a Custom UITableViewCell which has a UIlabel and a UITextField.

In my firstViewController i load and show the custom table:

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

    static NSString *CellIdentifier = @"Cell";

    LoginTableCell *cell = (LoginTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (LoginTableCell *) currentObject;
                break;
            }
        }       
    }

Then when the user hits "Login" i want to get the UITextField.text value from there and store it into a string.

- (IBAction)login:(id)sender {

I know this may sound simple, but i have been stuck on this, and can't seem to find the correct answer to solve.

I thought I could do something like this:

NSString *user = LoginTableCell.loginTextField.text;

but this returns an error of:

request for member 'text' in something not a structure or union

any help much appreciated

Thanks

Sam

Upvotes: 1

Views: 2506

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

In general you can do it like this. Make sure the indexPath in the below code is the correct indexPath for the row which has the required textField.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
LoginTableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *user = cell.loginTextField.text;

Upvotes: 1

Nipin Varma
Nipin Varma

Reputation: 384

IN tableview didSelectRowAtIndexPath delegate, you have to declare a variable for what is in the textfield,and u have to declare it in wherever you want by setting @property @synthesis and use this string as global variable.then u can use this string and wherever u want to display,by setting your textfield.text = your-string;

Upvotes: 0

Related Questions