brianhevans
brianhevans

Reputation: 1193

Sharing data between ViewControllers

I have a simple Single View iOS 5 application that I added a second view controller to. I embedded these inside a Navigation Controller and I placed two buttons on my FirstViewController. Each of these buttons have a named identifier for the segue that in turn displays my SecondViewController in the UI of the iPhone application.

All works as expected, BUT, I would like to display in a label on the second view controller something as simple as 'You clicked Button 1'. I have tried placing this data manually in a NSMutableString variable that I declare in my AppDelegate, which I am able to reach in my second view controller but the value I assign to this is never displayed on the screen.

Apparently, a new instance of the SecondViewController is created and this might be why I am not seeing it. I have created an IBOutlet of type UILabel with a name, myLabel, to hold this value but alas, I see no change.

Any help would be greatly appreciated.

NOTE: I am happy to post code but I don't think it will really help with my question.

Upvotes: 1

Views: 579

Answers (4)

user2330922
user2330922

Reputation: 141

You can also use notifications. In the IBAction of the first button of your FirstViewController, you can add

NSNotification *messageFromFirstViewController = [NSNotification notificationWithName:@"firstbuttonMessage" object:@"You clicked Button 1"]; [[NSNotificationCenter defaultCenter] postNotification:messageFromFirstViewController];

and something similar for the IBAction of your second button.

In the viewDidLoad of your SecondViewController, you can then add

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(useNotification:) name:@"firstButtonMessage" object:nil];

Create the useNotification method to change your myLabel:

-(void) useNotification:(NSNotification *)notification {
self.myLabel.text = notification.object;}

Upvotes: 0

ferus
ferus

Reputation: 376

Do you use Storyboards? Anyways, have you connected IBOutlet to your variable in Interface Builder?

If you use Storyboards, you might try to use method called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

ie.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ChooseToDetailsSegue"]) {
        preparedSegue = segue;
        SaleDetailsViewController *saleDetailsVC = [preparedSegue destinationViewController];
        saleDetailsVC.saleDetailsProduct = [elementsArray objectAtIndex:selectedRow];
        saleDetailsVC.cardTransactionDetails = [[cardDetails alloc] init];
        saleDetailsVC.cardTransactionDetails.number = infoCardNumber;
        saleDetailsVC.cardTransactionDetails.month = infoExpiriationM;
        saleDetailsVC.cardTransactionDetails.year = infoExpiriationY;     
    }
}

You just need to init new ViewController (your destination) and refer to their variables.

Upvotes: 0

CodaFi
CodaFi

Reputation: 43330

My solution in this situation is always a custom -init method. Such as,

-initWithButtonPressedString:(NSString*)message;

Declare that in the second view controller and call:

self.secondView = [[SecondViewController alloc]initWithButtonPressedString:@"some conditional string"];

Then, all that's required is an iVar in the second view controller to handle that passed string.

Upvotes: 2

rooster117
rooster117

Reputation: 5552

To pass data between view controllers you should consider 1. making a custom init for your view controller which passes in the additional information or 2. creating properties on your second view controller which you access from the first view controller.

You can create a property to the IBOutlet that you made but you need to make sure that if you access it from your first view controller that it is after the views are loaded.

It is hard to give you more direction without seeing your current code

Upvotes: 1

Related Questions