When a button is clicked at FirstVC
, it will pass data and trigger SecondVC
using NSNotificationCenter
During initial launch of the app, because SecondVC
has not been initialize yet, so data cannot be passed to SecondVC
. NSNotificationCenter
cannot function properly. Only after SecondVC
has been initialize, NSNotificationCenter
will function correctly.
So I need to initialise SecondVC
somewhere. Will it be at - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
?
Or how do I programatically call the tab of SecondVC
.
#import "Search.h"
#import "Classes.h"
#import "MyTabBarController.h"
@interface Search(){
AppDelegate *appDelegate;
CERangeSlider* _rangeSlider;
NSString *sURL, *strResult, *sRemaining, *sStartTime, *sEndTime, *sSelectedLat, *sSelectedLong;
}
@end
@implementation Search
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)btnSearch:(UIButton *)sender {
self.tabBarController.selectedIndex = 1;
sURL = @"Testing 123";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:sURL forKey:@"theURL"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"toClasses" object:nil userInfo:userInfo];
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(receiveTestNotification:)
name:@"toClasses"
object:nil];
dtDate = [[NSMutableArray alloc] init]; //=== Mutable array to store the dates generated
self.currentPageIndex = 0;
[self setupSegmentButtons];
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
sDtDate = [dateFormatter stringFromDate:now];
[self LoadClasses];
}
-(void)viewWillAppear:(BOOL)animated {
//--- Hide the Top Navigation Controller Bar at the current View
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
//--- Top Navigation Controller reappear on the next VC
-(void)viewDidDisappear:(BOOL)animated{
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
-(void) receiveTestNotification:(NSNotification*)notification
{
if ([notification.name isEqualToString:@"toClasses"])
{
NSDictionary* userInfo = notification.userInfo;
NSLog (@"Successfully received userInfo! %@", userInfo);
NSString* sFromSearch = [NSString stringWithFormat: @"%@", userInfo];
NSLog (@"Successfully received test notification! %@", sFromSearch);
}
}
In my opinion, you don't need to use notification or singleton on this case.
Simply, get SecondViewController
from self.tabBarController
and call the method.
- (IBAction)btnSearch:(UIButton *)sender {
self.tabBarController.selectedIndex = 1;
sURL = @"Testing 123";
UINavigationController* secondNav = (UINavigationController*)self.tabBarController.viewControllers[1];
SecondViewController* secondViewController = [secondNav.viewControllers firstObject];
[secondViewController handleString:sURL];
}
- (void)handleString:(NSString*)string {
// Do whatever you want with string passed from First VC
}