0

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 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

OK. Can I create NSString class files for characterString and other one or two data? will I be able to get my data from that in any way?
@downvoter Dont see whats wrong with this. Maybe because global variables are a bad idea. At least leave a comment. +1
Sure! All variables in app delegate will live the all time until you app will live.
@SergeyNeskoromny Is there a way I can access instance variables present in the view controller that presented another view controller over itself?
2

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 :)

Comments

0

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];

1 Comment

OK. Can I create NSString class files for characterString and other one or two data? will I be able to get my data from that in any way?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.