Ios 在ViewController之间共享变量
我有一个iPhone应用程序,有三个页面,每个页面都允许用户输入一些文本。在最后一页,我想连接所有三个字符串并将其打印出来。每个页面都有一个Ios 在ViewController之间共享变量,ios,objective-c,viewcontroller,Ios,Objective C,Viewcontroller,我有一个iPhone应用程序,有三个页面,每个页面都允许用户输入一些文本。在最后一页,我想连接所有三个字符串并将其打印出来。每个页面都有一个UIViewController(名为PageXController),我试图将变量传递到最后一页。我目前尝试的方法不太管用。以下是一个例子: 我首先在PageThreeController.h @interface PageThreeController : UIViewController{ NSMutableString *string; }
UIViewController
(名为PageXController
),我试图将变量传递到最后一页。我目前尝试的方法不太管用。以下是一个例子:
我首先在PageThreeController.h
@interface PageThreeController : UIViewController{
NSMutableString *string;
}
@property (nonatomic) NSMutableString *string;
接下来,我将以下内容添加到PageOneController.h
#import "PageThreeController.h"
@interface PageOneController : UIViewController
@property (nonatomic) PageThreeController *pageThree;
在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);
}
NSLog
在第一页打印出我的字符串是'Hello World'
。但是当我在连接之前在PageThreeController.m
上添加相同的NSLog时,'string'
是NULL
似乎我正在制作
pageThreeViewController
的单独副本。我需要做什么来更改第三页上字符串的实际值?我在这方面真的是新手定制您的prepareForSegue方法,以便通过这些控制器的公共属性传递必要的变量。
例如:
#pragma mark - PreparaForSegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"goToDetail"]) {
YourDestinationController *destinationController = segue.destinationViewController;
destinationController.myString = self.myStringToPass;
}
}
其中myString是DestinationController中的公共属性,myStringToPass是源控制器中的属性(它可能在私有范围内)如果需要从任何地方访问它,可以将变量存储在AppDelegate中。因此,如果在AppDelegate中有这样一个变量:
@property (nonatomic) NSString *currentStringToPass;
然后,您可以使用以下代码从ViewController访问它:
- (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]);
}
在ViewController之间传递数据的最简单方法是使用AppDelegate,即使 还有其他方法 方法1-使用AppDelegate。 在Appdelegate中添加以下行
@property (strong,nonatomic) NSMutableString *str;
要从任何视图控制器访问变量
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
NSMutableString *Stringdata=appDelegate.str;
方法2-指定对象。
在这个方法中,您可以像现在这样继续,只需要指定
查看控制器实例
让您能够从一个控制器导航到另一个控制器,比如说从第一个控制器导航到第二个控制器。
第一控制器
@interface FirstController : UIViewController{
NSMutableString *string;
}
@property (nonatomic) NSMutableString *string;
@interface SecondController : UIViewController{
}
@property (strong,nonatomic) FirstController *firstScreen;
第二控制器
@interface FirstController : UIViewController{
NSMutableString *string;
}
@property (nonatomic) NSMutableString *string;
@interface SecondController : UIViewController{
}
@property (strong,nonatomic) FirstController *firstScreen;
在FirstController的实现中,在导航到SecondController之前,必须在SecondController中指定实例
在FirstController.m中
SecondController *nextScreen=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"];
nextScreen.firstScreen=self;
然后在SecondController.m中,您只需将字符串作为
_firstScreen.string;
\u pageThree.from=[[NSMutableString alloc]init]代码>启动错误;调用代码不应分配实例变量。是否使用导航控制器?如何在视图控制器之间切换?可能与“Related”(相关)下的答案重复或有十几个其他答案------->因此,如果实例变量的唯一更改时间是在不同的页面上,那么您打算在哪里分配实例变量?我正在尝试您的方法1。当我在p1中打印出appDelegate.str时,它为空。但当我将appDelegate.str=((MutableString alloc)init)添加到它时,我可以在p1中打印出appDelegate.str。然而,当我试图在p3中访问它并将其打印出来时,我得到null。这与我如何分配有关吗?我应该在哪里分配appDelegate.str?请尝试在应用程序:(UIApplication*)中的appDelegate.m中启动它。应用程序已完成使用选项启动。您必须使用方法2进行对象引用。ie访问viewcontroller实例而不创建新实例。我认为现在一切正常。谢谢你的详细解释。我可以看到应用程序委派对我来说是多么容易。我计划稍后传递布尔数组,这将非常有用