TMan
TMan

Reputation: 4122

How to call method from a tabbar rootview controller

I have 3 tabbar items, each a view controller. First, Second, and Third viewcontroller. I need First ViewController to call a method that updates the tableview on second view controller view. I'm not sure what's the correct way to handle this. I tried a sharedInstance, but what I think is happening is two instances are being created and that view controller that the first VM is using isn't the same VM that is actually being used in the app, which would explain why my tableview isn't updating.

Basically when I upload a file in First View Controller, I need the Second VM to update the tableview and show the file's upload progress. Kind of like when a song is purchased on iTunes. These are UINavigationViewControllers for tab items.

I tried this:

+ (SecondViewController *)sharedInstance {
// Singleton implementation
static SecondViewController* instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    instance = [[SecondViewController alloc] init];
});

return instance;
}

 - (void)viewDidLoad
 {
  [super viewDidLoad];
  UploadTableView.dataSource = self;
  UploadTableView.delegate = self;

  [S3UploadClientManager tm].delegate = self;

 }

Upvotes: 0

Views: 85

Answers (2)

terry lewis
terry lewis

Reputation: 672

You could implement the tabBarController delegate method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if([viewController isMemberOfClass[SecondViewController class]]) {
//pass the data here
}
}

Upvotes: 0

aaronman
aaronman

Reputation: 18761

You don't want the controllers to communicate with each other directly. If you are segueing to another view you can use prepareForSegue. If you don't want to use that I suggest you either update a file or a database that both controllers have access to as to avoid direct interaction and keep the mvc architecture.

Upvotes: 1

Related Questions