0

I have the following Storyboard. Pic

I wish to pass a string say sURL to Classes VC. Classes VC is embedded in a Tab View Controller and a Navigation Controller. Your help is gladly appreciated.

Hanz Cheah
  • 761
  • 5
  • 15
  • 44
  • you can use custom delegate or notificationcenter to communicate with viewcontroller – Vinodh Mar 08 '18 at 06:41
  • Can you show me an example please, how about directing to another view controller? is it something like this UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UITabBarController *tabView = [storyboard instantiateViewControllerWithIdentifier:@"profileView"]; [self presentViewController:tabView animated:YES completion:nil]; but profile view is initial my TabView Controller, how do I direct to Classes VC? – Hanz Cheah Mar 08 '18 at 06:45
  • Custom delegate : https://stackoverflow.com/a/12660523/1142743. notificationcenter : https://stackoverflow.com/a/2191802/1142743 or use UserDefaults to store the value that's the simplest way – Vinodh Mar 08 '18 at 06:47
  • I think i got it will post a solution later, Thanks Vinodh, I just need to name my storyboard. – Hanz Cheah Mar 08 '18 at 06:58
  • okay you can post your own solution – Vinodh Mar 08 '18 at 07:19
  • Use the custom property or [segue](https://www.google.com/search?q=ios+segue+pass+data&oq=ios+segue). DO NOT use the `NSNotificationCenter` or `NSUserDefaults` please! – Itachi Mar 09 '18 at 01:47
  • Hi Itachi, the `prepareForSegue:` method to pass value only works between 2 controllers that pushes? Does it work with VC that is beyond not pushed? – Hanz Cheah Mar 09 '18 at 04:13

1 Answers1

-1

Store what ever string that you want to pass into NSDefaults and then at the other VC call the NSDefaults again.

//=== Strore into session and send over to Classes VC
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:sURLToClass forKey:@"txtURLFromSearch"];
[defaults synchronize];

//=== This is to call Classes VC
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabView = [storyboard instantiateViewControllerWithIdentifier:@"profileView"];
tabView.selectedIndex=0; //=== This is to choose which Tab, starts with 0,1,2,3,4
[self presentViewController:tabView animated:YES completion:nil];
Hanz Cheah
  • 761
  • 5
  • 15
  • 44
  • Don't use `-[NSUserDefaults synchronize]`. From [Apple's documentation](https://developer.apple.com/documentation/foundation/nsuserdefaults/1414005-synchronize?language=objc) _"this method is unnecessary and shouldn't be used."_ – Ashley Mills Oct 11 '18 at 14:22