FelipeDev.-
FelipeDev.-

Reputation: 3143

Update UITableView data

I have three ViewControllers:

RootViewController
FirstViewController
SecondViewController

From the RootViewController I create a TabBarController with the other two ViewControllers. So I need to do something like:

 FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

 SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

And then add the controllers to the TabBarController. In that moment, my two ViewControllers are instantiated.

To update the data I have tried this in my FirstViewController.m:

SecondViewController *test = [[SecondViewController alloc] init];
 [test.tableView reloadData];

But nothing happens, I suppose because my SecondViewController has been allocated before, and am creating a new instance of it.

How can I update the data of my Table in the SecondViewController from my FirstViewController?

Upvotes: 0

Views: 217

Answers (4)

SachinVsSachin
SachinVsSachin

Reputation: 6427

You can access secondViewController this way:

UITabBarController *tabController = (UITabBarController *)[self parentViewController];

[tabController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    if ([obj isKindOfClass:[SecondViewController class]]) {
        SecondViewController *secondController = obj;
        [secondController.tableView reloadData];
       *stop = YES;

    }
    
}];

Upvotes: -1

Chris Tetreault
Chris Tetreault

Reputation: 1973

If you're trying to update the data used in the second view controller without needing to switch I would use the delegate pattern.

Create a protocol declaration in FirstViewController like

@class FirstViewController;

@protocol FirstViewControllerDelegate

-(void) updateDataFromFirstViewController: (NSMutableArray*)newArray;

@end

@property (strong, nonatomic) id<FirstViewControllerDelegate>delegate;

then in your firstViewController when you have new data to update call

[self.delegate updateDataFromFirstViewController:yourNewData];

implement this method in SecondViewController.m, and add the delegate to the SecondView Controller.h

SecondViewController: UIViewController <FirstViewControllerDelegate>

Then in

-(void) viewWillAppear:(BOOL)animated

reload your table data, so that when you actually need to see the updated data, its there when you switch. Also don't forget to set the FirstViewController delegate to self in the SecondViewController.

Upvotes: 1

brianLikeApple
brianLikeApple

Reputation: 4371

Does your TabBarController hold FirstViewController and SecondViewController? In the TabBarController, there is a property - viewControllers. It is an array so you can use it to access your viewControllers.

[tabBarController.viewControllers objectAtIndex:0];//This is your First viewController
[tabBarController.viewControllers objectAtIndex:1];//This is your Second viewController

And then access sencondViewController's tableView and reload it.

Hopes this is helpful.

Upvotes: 0

Owen Hartnett
Owen Hartnett

Reputation: 5935

Have your root view controller pass the value of viewController2 down to viewController1 when it creates them. Describe the properties as weak, as you want rootController to own them, not the other viewController.

Upvotes: 1

Related Questions