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