Dipanjan Dutta
Dipanjan Dutta

Reputation: 33

Retrieve data from text field

I have a text field in my app. I am trying to store whatever is entered into the text field in an array and display it in my root view controller (which is a table view) on click of a button.

The method for the button is as follows:

-(IBAction)addNewCountry:(id)sender
{

    [rootViewController.details addObject:nameField.text];
    NSLog(@"Country name is %@", rootViewController.details);
    [self.navigationController pushViewController:rootViewController animated:YES];
    [rootViewController.tableView reloadData];
    NSLog(@"new country added");
}

details is the array declared in RootViewController

However, the text field text is not retrieved. Can anybody tell me what am i missing?

Upvotes: 0

Views: 1083

Answers (3)

ScottK
ScottK

Reputation: 121

Is your details array declared as NSMutableArray? It must be declared as an NSMutableArray in order for it to be modifiable. Also when you initialized the array did you add this after the allocation:

[details retain];

Upvotes: 0

Marios
Marios

Reputation: 509

You cont directly insert your string data to your array, you need to store as a string

NSString *stringval = [NSString stringWithString: nameField.text];

then add it in to your string using addobject

[rootViewController.details addObject: stringcal];

Upvotes: 0

dasdom
dasdom

Reputation: 14063

Change this

[rootViewController.details addObject:nameField.text];

to this

NSString *name = [NSString stringWithString: nameField.text];
[rootViewController.details addObject: name];

Upvotes: 1

Related Questions