简体   繁体   中英

Sharing a variable between ViewControllers

I have an iPhone app with three pages, each of which allows the user to enter some text. On the final page I want to concatenate all three strings and print it out. I have a UIViewController (named PageXController ) for each page and I am trying to pass variables along to the final page. The method I currently try doesn't quite work. Here is an example:

I begin by declaring a string as an instance variable in PageThreeController.h

@interface PageThreeController : UIViewController{
    NSMutableString *string;
}

@property (nonatomic) NSMutableString *string;

Next I add the following to PageOneController.h ,

#import "PageThreeController.h"

@interface PageOneController : UIViewController

@property (nonatomic) PageThreeController *pageThree; 

In PageOneController I then attempt to set the string instance variable on page three;

- (IBAction)handleButton:(id)sender {
    _pageThree = [[PageThreeController alloc] init];
    _pageThree.from = [[NSMutableString alloc]init];
    [_pageThree.from appendString:@"Hello World"];
        NSLog(@"My string is %@ on page one.", _pageThree.from);
}

The NSLog prints out My string is 'Hello World' on page one. but when I add the same NSLog on PageThreeController.m before concatenating, 'string' is NULL .

It seems that I am making a separate copy of the pageThreeViewController . What do I need to do to change the actual value of string on page three? I am really new at this

Customize your prepareForSegue method in order to pass necessary vars through public properties of these controllers. For example:

#pragma mark - PreparaForSegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if ([segue.identifier isEqualToString:@"goToDetail"]) {
           YourDestinationController *destinationController = segue.destinationViewController;
            destinationController.myString = self.myStringToPass;
    }
}

Where myString is a public property in YourDestinationController and myStringToPass is a property in your source controller (it could be in private scope)

If you need to access it from anywhere, you could store the variable in the AppDelegate. So if you had a variable like this in your AppDelegate:

@property (nonatomic) NSString *currentStringToPass;

Then you could access it from your ViewControllers by using the following code:

- (IBAction)handleButton:(id)sender {
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    _pageThree = [[PageThreeController alloc] init];
    _pageThree.from = [[NSMutableString alloc]init];
    [app setCurrentStringToPass:@"Hello World"];
        NSLog(@"My string is %@ on page one.", [app currentStringToPass]);
}

The easiest way to pass data between ViewControllers is using the AppDelegate, even though there is other methods .

Method 1 - using AppDelegate. Add the following line in your Appdelegate.

@property (strong,nonatomic) NSMutableString *str;

To access the variable from any view controller,

 MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate]; 

 NSMutableString *Stringdata=appDelegate.str;

Method 2 -Specifying objects. in this method , you can proceed as you are now doing and, just need to specify the view controller instance.

let you have are navigating from one controller to another , say FirstController to Second. Firstcontroller.h

@interface FirstController : UIViewController{
    NSMutableString *string;
}

@property (nonatomic) NSMutableString *string;

SecondController.h

@interface SecondController : UIViewController{

}

@property (strong,nonatomic) FirstController *firstScreen;

Within your implementation of the FirstController, before you navigate to the SecondController ,you have to specify the instance in SecondController.

in FirstController.m

SecondController *nextScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"];

nextScreen.firstScreen=self;

Then in your SecondController.m , you can simply get the String as

_firstScreen.string;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM