How to initialise a VC or Class iOS, ObjectiveC

134 Views Asked by At

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.

FirstVC

#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

Second VC

- (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);
    }
}
2

There are 2 best solutions below

3
trungduc On BEST ANSWER

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.

First VC

- (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];
}

Second VC

- (void)handleString:(NSString*)string {
  // Do whatever you want with string passed from First VC
}
0
Jabson On

You added observer in viewDidLoad, so it will not work even you create it before user tap on button and send notification. because observer will not be registered. I advise you not use observer to send data in this case. you can save this data elsewhere and use it when seconVC will load. for example in singleton object.

your Singleton object looks like this:

Interface:

@interface DataManager : NSObject
@property (nonatomic, strong) NSDictionary *userInfo;
+ (DataManager *) getInstance;
@end

Implementation:

@implementation DataManager

+ (DataManager *) getInstance {
    static DataManager *appManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appManager = [[DataManager alloc] init];
    });

    return appManager;
}

@end

Now you can access this object where you want and you can assured that only one instance is created.

here is your button click method:

- (IBAction)btnSearch:(UIButton *)sender {
    self.tabBarController.selectedIndex = 1;
    sURL = @"Testing 123";
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:sURL forKey:@"theURL"];
    [DataManager getInstance].userInfo = userInfo;
}

and your viewDidLoad in secondVC

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *userInfo = [DataManager getInstance].userInfo;
}