tags:

views:

198

answers:

2

Hey, I had this question, http://stackoverflow.com/questions/2097788/uitableview-not-refreshing-after-modal-view-controller-dismisses

But, I didnt seem to get a straight answer,

Can anyone else shed some light?

Heres the code.

FirstViewController.h

@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

NSMutableArray *routines;
IBOutlet UITableView *myTableView;

}

 @property (nonatomic, retain) NSMutableArray *routines;
 @property (nonatomic, retain) UITableView *myTableView;

- (IBAction)showNewEventViewController;   

@end

and its .m

#import "FirstViewController.h"
#import "NewEventViewController.h"

@implementation FirstViewController

@synthesize routines, myTableView;


- (void)viewWillAppear:(BOOL)animated{
[myTableView reloadData];
NSLog(@"Routines: %@", routines);
NSLog(@"refreshed!");

for(int i = 0; i < [routines count]; i++){

    NSLog(@"%@", [routines objectAtIndex:i]);

          }

 }  




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [routines count];

}


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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
NSString *cellValue = [routines objectAtIndex:indexPath.row];
[cell.textLabel setText:cellValue];

return cell;
}



- (IBAction)showNewEventViewController {    

NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventView" bundle:nil];
controller.routines = routines;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];

[controller release];
}




- (void)viewDidLoad {

routines = [[NSMutableArray alloc] init];

[routines addObject:@"Hello"];
[routines addObject:@"Temp"];
[routines addObject:@"Temp2"];
[routines addObject:@"Temp3"];
[routines addObject:@"Temp4"];


}

and the modal view controllers .h (NewEventViewController.h)

#import <UIKit/UIKit.h>


 @interface NewEventViewController : UIViewController {

IBOutlet UITextField *RoutineTitle;

IBOutlet UITextField *RoutineInvolvment;


NSMutableArray *routines;


}


@property(nonatomic, retain)  NSMutableArray *routines;


-(IBAction)done;


@end

and its .h

#import "NewEventViewController.h"
#import "FirstViewController.h"




@implementation NewEventViewController
@synthesize routines;


-(IBAction)done{



 [routines addObject:RoutineTitle.text]; 
[self dismissModalViewControllerAnimated:YES];
NSLog(@"Routines: %@", routines);

}
A: 

You keep asking the same question, so apparently there is a disconnect somewhere. Post your code that you are using to present the modal view controller. The workflow should go something like this:

  • Create view controller you're going to push modally. Makes sure it has an ivar and accessors for setting your table view's data container (e.g. setDataContainer:(NSMutableArray*)dataContainer)

  • Call setDataContainer:routines on the view controller.

  • Present the modal view controller

  • In your done button, add the record to your data container that you set ([dataContainer addObject:newObject], and dismiss the modal view controller

  • Call reloadData in your viewWillAppear as you're doing in the code from your link above.

We can't help you if you don't post some code that shows what you're doing.

Best Regards,

Matt Long
ive added the code. I wrote it that way on advice from another answer. So, if you see the errors in it, then please correct it. But, it seems to add the string to the array, from the MVC,but it does not reload it. :(
Sam Jarman
Your code looks fine. The only things I see are your viewDidLoad doesn't call [super viewDidLoad]; Also, is it possible that your table view outlet got disconnected? That would explain why it's not actually updating when you call reloadData. I created a project myself that does what you are wanting without a problem. Take a look at it and compare to what you have. Then let me know what the difference was. At the moment, I can't see it: http://www.cimgf.com/files/CreateModalRecord.zip
Matt Long
i changed some things in mine, but not really any difference. Would you be annoyed if I just built on your project?
Sam Jarman
im 90% sure it was the fact that it was not a table view template.
Sam Jarman
Im having trouble turning it into a tab bar based application. Any ideas?
Sam Jarman
Im still having trouble. I do appreciate what you have done so far Matt. But would it be too much to ask if you convert this into a tab bar application? I dont know how to do it. (at all)
Sam Jarman
This blog post I wrote might help: http://www.cimgf.com/2009/06/25/uitabbarcontroller-with-uinavigationcontroller-using-interface-builder/
Matt Long
ah. thats what i'm after, sort of. Cheers
Sam Jarman
A: 

Your done selector needs to call reloadData. If everything is wired properly (the table view delegate, dataSource) it should work properly. You could however set the view hidden and re-appear until it works but its too hackish (and not the right approach).

Your done is part of the NewEventViewController so you can either:

1 Pass the data via using the NSNotificationCenter defaultCenter

2.Make the FirstViewController instance a global that is accessible from the done selector.

David Sowsy