0

This is my code, it works when I send a Label value but when I Pass as string it shows null please help me out... Suggest me a way to pass a String to the second view

This is My First vc "AssignmentsDetailsJson.j"

#import <UIKit/UIKit.h>
#import "DocumentsViewController.h"

@interface AssignmentsDetailsJson : <UITabBarControllerDelegate,UITabBarDelegate>

@end

This is the AssignmentsDetailsJson.m file

#import "AssignmentsDetailsJson.h"

@interface AssignmentsDetailsJson ()
@property(strong, nonatomic) DocumentsViewController *controllerB;

@end

@implementation AssignmentsDetailsJson

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.controllerB = (DocumentsViewController *)                                    [tabBarController.viewControllers objectAtIndex:1];

    self.controllerB.data= @"yooooooooooo!";

}

- (void)viewDidLoad 
{
   [super viewDidLoad];

   self.tabBarController.delegate = self;

}

This is the second view DocumentsViewController.h

#import <UIKit/UIKit.h>
#import "AssignmentsDetailsJson.h"

@interface DocumentsViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *aLabelInControllerB;
@property (nonatomic, strong) NSString *data;

@property(strong, nonatomic) DocumentsViewController *controllerB;
@end

This is the DocumentsViewController.m

#import "DocumentsViewController.h"

@interface DocumentsViewController ()


@end

@implementation DocumentsViewController
@synthesize data;
- (void)viewDidLoad 
{
    [super viewDidLoad];

     NSLog(@"hooo %@",data);
}

- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
Nan
  • 35
  • 6
  • this may help you, http://stackoverflow.com/questions/21271681/passing-data-to-tab-bar-controller . You should add more details about how you're using your tabBar – enzo Nov 05 '15 at 10:41

2 Answers2

0

You could do that using delegate or NSNotification with more risks than delegate.

Community
  • 1
  • 1
Nghia Luong
  • 790
  • 1
  • 6
  • 11
0

the problem is that viewDidLoad gets called before you have passed the data. that is why you do not get any output in your NSLog statement. instead of tabBarControllerDidSelect[...] try the following:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  if ([viewController isKindOfClass:[SecondViewController class]]) {
    ((SecondViewController *)viewController).data = @"passed some data";
  }
  return YES;
}
André Slotta
  • 13,774
  • 2
  • 22
  • 34