Bebhead
Bebhead

Reputation: 209

how to pass information between scenes objective c

I'm completely new to objective c and ios development, I'm simply trying to navigate to a new scene on my storyboard, and upon navigating change the text of a UIlabel to something.

The way I have this set up is I have a View controller and a tableview controller, each with their own header and implementation files. I'm using a segue from a button on the view controller to navigate to the tableview.

I did read this stack overflow post (How do I pass information between storyboard segues?) which explains how to pass values on a segue and have implemented it as such:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSLog(@"prepareForSegue: %@", segue.identifier);
    if([segue.identifier isEqualToString:@"navtotablebut"])
    {
        TableViewController *testcontroller = segue.destinationViewController;
        testcontroller.testpass.text = @"Testing123";


        NSLog(@"%@",[NSString stringWithFormat:@"testpass has been changed to %@",      testcontroller.testpass.text]);
    }
}

The logs verify that this segue method does get called, and it is able to correctly identify the id I gave it (navtotablebut). I then attempt to set the value of my UIlabel's text property on my tableview controller to a simple string "Testing123". However not only does this change not reflect on the new screen, but my print statement afterwards seems to think testcontroller.testpass.text is equal to (null).

Am I doing something horribly wrong here?

Upvotes: 1

Views: 1008

Answers (3)

Apoorv
Apoorv

Reputation: 412

You are setting the value of label testcontroller in TableViewController before the view of `TableViewController ' is drawn.

Please note: update the view in viewdidLoad or viewDidLayoutSubviews if using Autolayout.

Best way is pass your value as a string to other view controller and then populate.

Upvotes: 2

karthika
karthika

Reputation: 4091

In TableViewController, You need to create property,

TableViewController.h

@property (nonatomic, strong) NSString *subject;

you pass value like that,

In ViewController,

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier]isEqualToString:@"Detail"])
    {
       TableViewController *testcontroller = segue.destinationViewController;
       testcontroller.testpass.text = @"Testing123";
        detailView.subject = testcontroller.testpass.text;

    }
}

Upvotes: 0

D-eptdeveloper
D-eptdeveloper

Reputation: 2430

Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

also with performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.

// When any of my buttons are pressed, push the next view
-(IBAction)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"MySegue" sender:sender];
}

// This will get called too before the view appears
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        // Get destination view
        SecondView *vc = [segue destinationViewController];

        // Get button tag number (or do whatever you need to do here, based on your object
        NSInteger tagIndex = [(UIButton *)sender tag];

        // Pass the information to your destination view
        [vc setSelectedButton:tagIndex];
    }
}

Upvotes: 1

Related Questions