lpasztor
lpasztor

Reputation: 167

Passing data between ViewControllers is working, but not with Tab Bar Controller

Could anyone please help me in the following problem?

I new in Xcode, I'm still learning it, but I could figure out how to pass data between ViewControllers.

Here is my working code:

FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
- (IBAction)displayView:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *lblFirst;


@end

FirstViewController.m:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize lblFirst;
SecondViewController *secondViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    secondViewController.forwarded_lblFirst = lblFirst;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)displayView:(id)sender {


    [self.view addSubview:secondViewController.view];
}
@end

SecondViewController.h:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (nonatomic, retain) UILabel *forwarded_lblFirst;
@property (weak, nonatomic) IBOutlet UITextField *txtSecond;
- (IBAction)btnReturn:(id)sender;
- (IBAction)txtSecond_DidEndOnExit:(id)sender;
- (IBAction)btnSecond:(id)sender;

@end

SecondViewController.m:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize forwarded_lblFirst;
@synthesize txtSecond;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnReturn:(id)sender {
    [self.view removeFromSuperview];
}
- (IBAction)txtSecond_DidEndOnExit:(id)sender {
    forwarded_lblFirst.text = txtSecond.text;
    [txtSecond resignFirstResponder];
    [self.view removeFromSuperview];
}

- (IBAction)btnSecond:(id)sender {
    forwarded_lblFirst.text = txtSecond.text;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [txtSecond resignFirstResponder];
}

@end

This code is working perfectly, the text what I enter on the SecondViewController's textbox (after pressing the btnSecond or simply the Return key on the keyboard) appears as the text of the Label on the FirstViewController.

My problem is, that when I do the exact same thing, but with a Tab Bar Application, the Label on the First tab won't change.

I can use the exact same code (except the changing-views part, because I handle on the first app with programmed buttons, but for the Tab-Bar-App there are already the buttons), there are also two ViewControllers for the Tab Bar App, too (one for each tabs). So the code is the same, line by line, character by character, and the files are the same, too, for both apps (View-Based, Tab-Bar).

Why isn't my code working? How can I solve this problem? Thank you!

Upvotes: 2

Views: 1855

Answers (3)

Elto
Elto

Reputation: 429

You need to use tabbardelegate in your secondview controller, assuming that you use this schema

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (strong, nonatomic) NSString *content;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tabBarController.delegate = self;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

    if ([viewController isKindOfClass:[FirstViewController class]]){
        FirstViewController *vc =( FirstViewController *) viewController;
        vc.content = @"your content";
    }


    return TRUE;
}

Upvotes: 0

Phillip Mills
Phillip Mills

Reputation: 31026

A different approach to this problem is to use an independent data model.

Create an object that provides access and manipulation methods for all the data that your application needs to save, load, share, or use in any non-trivial way. Make the object available either as a Singleton or as a property of the application delegate. When changes happen, have the data model update application state. When something needs to be displayed, have the controller fetch it from the data model and send it to the view (MVC!).

If you don't store things in controllers that actually belong in the model, you never need to worry about passing information from controller to controller.

Upvotes: 1

Vlad Z.
Vlad Z.

Reputation: 3451

You need to read some articles about transferring data between views:

Talking about your problem, try this approach:

Add the property to your Application Delegate.

When assigning the property do something like:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.myProperty = @"My Value";

then, in your different tabs, you can retrieve this property in the same manner:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty; 

Upvotes: 4

Related Questions