简体   繁体   English

在ViewController之间共享变量

[英]Sharing a variable between ViewControllers

I have an iPhone app with three pages, each of which allows the user to enter some text. 我有一个包含三个页面的iPhone应用程序,每个页面都允许用户输入一些文本。 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. 我为每个页面都有一个UIViewController (名为PageXController ),并且我试图将变量传递到最后一页。 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 我首先在PageThreeController.h字符串声明为实例变量。

@interface PageThreeController : UIViewController{
    NSMutableString *string;
}

@property (nonatomic) NSMutableString *string;

Next I add the following to PageOneController.h , 接下来,我将以下内容添加到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; 然后,我在PageOneController尝试在PageOneController页上设置字符串实例变量。

- (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. NSLog在第一页上打印出我的字符串是'Hello World' but when I add the same NSLog on PageThreeController.m before concatenating, 'string' is NULL . 但是当我在串联之前在PageThreeController.m上添加相同的NSLog时, 'string'NULL

It seems that I am making a separate copy of the pageThreeViewController . 看来我正在制作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. 自定义您的prepareForSegue方法,以通过这些控制器的公共属性传递必要的var。 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) 其中myString是YourDestinationController中的公共属性,而myStringToPass是源控制器中的属性(可以在私有范围内)

If you need to access it from anywhere, you could store the variable in the AppDelegate. 如果您需要从任何地方访问它,则可以将变量存储在AppDelegate中。 So if you had a variable like this in your AppDelegate: 因此,如果您的AppDelegate中有这样的变量:

@property (nonatomic) NSString *currentStringToPass;

Then you could access it from your ViewControllers by using the following code: 然后,您可以使用以下代码从ViewControllers中访问它:

- (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 . 在ViewController之间传递数据的最简单方法是使用AppDelegate,即使还有其他方法也是如此。

Method 1 - using AppDelegate. 方法1-使用AppDelegate。 Add the following line in your Appdelegate. 在您的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. 方法2-指定对象。 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到Second。 Firstcontroller.h Firstcontroller.h

@interface FirstController : UIViewController{
    NSMutableString *string;
}

@property (nonatomic) NSMutableString *string;

SecondController.h 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. 在FirstController的实现中,在导航到SecondController之前,必须在SecondController中指定实例。

in FirstController.m 在FirstController.m中

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

nextScreen.firstScreen=self;

Then in your SecondController.m , you can simply get the String as 然后在SecondController.m中,只需将String作为

_firstScreen.string;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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