简体   繁体   中英

Send object to UIViewController from UITabBarController

I have an object that is sent from my main VC to my MasterTabViewController(UITabBarController) in the viewDidLoad I NSLog the object and it shows the object, good. Now I need that object to go to the first tab UIViewController. I tried multiple times and cannot get it to go. I am new so forgive my ignorance,

I send the object from my main vc to my MasterTabViewController via segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showLogin"])
    {
        MasterTabViewController *preview = segue.destinationViewController;
        preview.communityTapped = self.tempCommunity;

    }



}

^This works fine!^ self.tempCommunity is an instance community object.

MasterTabViewController.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    FirstTabViewController *firstvc;
    firstvc.communityTapped = self.communityTapped;
    NSLog(@"%@ !!!!!!!!! ",self.communityTapped.commDescription);

    // Do any additional setup after loading the view.
}

FirstTabViewController.h

@property (nonatomic, strong) IBOutlet UILabel *descriptionLabel;
@property (nonatomic, strong) Community *communityTapped;

FirstTabViewController.m

- (void)viewDidLoad
{
    self.descriptionLabel.text = self.communityTapped.commDescription;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

If anyone could help that would be greatly appreciated as I have tried and failed many times.

You can't set up IBOutlets to a tab bar controllers view controllers (and I see from your project that you never hooked them up). In your viewDidLoad for the tab bar controller, you can get a reference to any of its view controllers with the viewControllers property. So do something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.firstvc = self.viewControllers[0];
    self.firstvc.communityTapped = self.communityTapped;
    NSLog(@"%@ !!!!!!!!! ",self.communityTapped.commDescription);
}

I think the problem is in the viewDidLoad of your MasterTabViewController.h . You're creating the variable firstvc , but you're not setting the value to anything. So when you set the value of communityTapped in the next line, you're trying to set the value of communityTapped on nothing.

Option 1

If you've set up the tab in interface builder, you need to create an IBOutlet in your MasterTabViewController.h and connect it to your view controller. Something like the following:

@property (strong, nonatomic) IBOutlet FirstTabViewController *firstvc;

Then to set the value of the communityTapped , property, you would use something like this:

self.firstvc.communityTapped = self.communityTapped

Option 2

The other option would be to create the tab pragmatically, and add it to the view. I'm not sure quite what your setup is, but I imagine it would be something like this:

FirstTabViewController *firstvc = [[FirstTabViewController alloc] init]; firstvc.communityTapped = self.communityTapped; NSArray *viewControllers = [[NSArray alloc] initWithObjects:firstvc, nil]; [self.navigationController.tabBarController setViewControllers:viewControllers animated:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM