0
votes

I have two view controllers used in a Tab Bar Controller: FirstViewController and SecondViewController. I'm trying to call a method in the SecondViewController from the FirstViewController, but do this before the SecondViewController is loaded by selecting the Second Tab. Is this possible? I've tried notifications and delegates and can't seem to get it to work, unless I select the SecondViewController and run ViewDidLoad first, and then call it from the FirstViewController.

This is in objective-c and I'm trying to call setAutoModeTimer() in the SecondViewController.

Here is my code:

FirstViewController.h

#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate <NSObject>

- (void) setAutoModeTimer;

@end

@interface FirstViewController : UIViewController

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

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    SecondViewController * myViewController = [[SecondViewController alloc] init];
    [myViewController view];
    // Do any additional setup after loading the view, typically from a nib.
}

//- (void)loadView{[self.tabBarController.viewControllers makeObjectsPerformSelector:@selector(view)];}


- (IBAction)startTimerButtonPressed:(id)sender {

    [self.delegate setAutoModeTimer];

}
@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

SecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController () <FirstViewControllerDelegate>

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    FirstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
    firstVC.delegate = self;

}

- (void) setAutoModeTimer
{
    NSLog(@"Timer has started");
}

@end
1
executing method before viewDidLoad is not possible. if you want to pass some Data then you can save data to tabBarViewController and then you can fetch data from secondViewControllerNimit

1 Answers

0
votes

You don't need to keep references in each view controller for another one. What are you doing is creating new instances of UIViewControllers in each view controller.

   FirstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
   firstVC.delegate = self;

After viewDidLoad will be finished firstVC instance will be released. Because you created a local var. It is not the same viewController you have in tabBar.

You need to setup connection between view controllers once.

It can be done by setup your UITabBarController programmatically, not in stroryboard. You can setup viewControllers of UITabBarController once, and setup connections between them.

#import "SecondViewController.h"
#import "FirstViewController.h"

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
    FirstViewController *firstVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
    SecondViewController * secondVC = [[SecondViewController alloc] init];
    firstVC.delegate = secondVC;
    [listOfViewControllers addObject:firstVC];
    [listOfViewControllers addObject:secondVC];

    [self setViewControllers:listOfViewControllers
                                 animated:YES];

}