0
votes

I have a publicly declared NSString, NSString *characterString in my mainViewController.m and just before I perform a [self performSegueWithIdentifier:@"segueToFinal" sender:self]; I update this characterString with the latest data. like so:

characterString=[NSString stringWithFormat:@"final string %@",truncatedString];
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];

When the new view controller finishes appearing, the user will press a button some time that will call a method that will want the characterString and possibly other publicly declared instance variables from that previous view controller. I was trying [[mainViewController alloc]getCharacterString] (with getCharacterString being a method implemented in mainViewController) but that of course creates a new instance of mainViewController and does not solve the issue.

How Can I access the data currently in 'characterString' and other variables from the old view controller?

4

4 Answers

3
votes

Keep all you global variables in you AppDelegate:

YourAppDelegate.h

@interface BTAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) NSString * characterString;

@end

YourViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

    app.characterString = @"Hello"; 

}

YourSecondViewController.m

- (IBAction)pressButton:(id)sender{
YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

        app.characterString = @"Hello2"; 


}

etc.

2
votes

Try this out :

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end

Now in the mainViewController import the NewViewController class :

#import "NewViewController.h"
.......
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NewViewController *destinationViewController = [segue destinationViewController];
    destinationViewController.newCharacterString = self.characterString;
}

and you can access the value of the character string in your NewViewController through the newCharacterString property. HTH :)

0
votes

The only thing I can think of is passing the old data from the view controller along with the segue. Or, you could even pass the entire old view controller if you have multiple properties you want to keep.

In the new VC (the one being segued to), add a property that you want to keep from the old VC (the one being segued from) such as the NSString *.

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end

Then, while you are segue - ing, just pass the value to the new VC.

characterString=[NSString stringWithFormat:@"final string %@",truncatedString];

newViewControllerIWantToPassAValueTo.newCharacterString = characterString; 
// passes new string forward to View Controller

[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
0
votes

Why you dont pass this characterString to the secondViewController

In SecondViewController:

@interface SecondViewController:UIViewController
@property (strong, nonatomic) NSString *newCharacterString;
@end

In MainViewController:

[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];

And you have to add the preparePerform Segue to MainViewController

- (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"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}

And you could use newCharacterString in SecondViewController without callback to the MainViewController