views:

93

answers:

3

Hi,

I have a list of items, and in a modal view controller, i have what is effectively a 'New item' screen, where a user can type in a new thing for the list.

The list is a UITableView, with data source, NSMutableArray.

here is the code on the MVC

-(IBAction)done{

[RoutineTitle resignFirstResponder];
[RoutineInvolvment resignFirstResponder];

NSString *myString = RoutineTitle.text;
FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

NSLog(@"Log the String: %@", myString);

[FirstView.routines addObject:myString];

[self dismissModalViewControllerAnimated:YES];

}

But, then when returned to the regular screen, it does not show up. im not sure if its not adding it to the array correctly, or if the table view needs to refresh or what.

Any help would be appreciated. Thanks

Sam

+1  A: 

You should use reloadData on your `UITableView

i.e.

[myTableView reloadData];
Henrik P. Hessel
6 am here.. sorry, edited my post
Henrik P. Hessel
A: 

In your -viewWillAppear:(BOOL)animated method, after you call to super you should also call [self.tableView reloadData];

jbrennan
A: 

Your code doesn't make any sense. Are you calling -presentModalViewController in your FirstViewController? Why are you instantiating a new FirstViewController in your -done method?

What you need to do when you present your 'new item' view controller is pass it a reference to your table view and data container. Something like this:

NewItemViewController *controller = [[NewItemViewController alloc] init];
[controller setTableView:firstViewTableView];
[controller setDataContainer:routines];
[self presentModalViewController:controller];
[controller release];

Then, in your -done: action of your new item view controller you can add the string to the data container (NSMutableArray) and then reload the table view like this:

- (IBAction)done:(id)sender;
{
    NSString *myString = RoutineTitle.text;
    [dataContainer addObject:myString];
    [tableView reloadData];

    [self dismissModalViewController];
}

Your NewItemViewController will need two @syntheisze'd ivars like this:

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataContainer;

Let me know if you need clarification.

Matt Long
Hey Matt, thanks for your response. This part FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];was so the routine array could be used in both classes. A friend told me to do it that way.will your soloution do the same thing? add the string to the array, then reload the data?
Sam Jarman
and what does setTableView: do? and what do i put after the ':'
Sam Jarman
-setTableView: should just take a reference to your table view in the view controller that calls -presentModalViewController. If that view controller is a UITableViewController, then you can just pass it [self tableView]. If not, you need to create an outlet to the UITableView you're using in the view controller.
Matt Long