我有一个带变量的控制器,比如
NSString *str;在同一个控制器中,我在字符串str中分配userId。
我需要在另一个控制器的userId上面显示用户数据。
我应该如何在另一个控制器中获取userId的值?
下面是我的代码:
MainViewController.h
@property (nonatomatic) NSString *str;MainViewController.m
@synthesize str;
str = 1;现在在FirstViewController.h中
@property(nonatomatic, retain) MainViewController *mainCon;FirstViewController.m
@synthesize mainCon;
NSLog(@"user id is %@", mainCon.str);在日志中,我得到的值是空值。
发布于 2012-12-26 22:27:23
您最好的选择是使用prepareForSegue方法
在这个方法中,我通常有类似这样的东西
if ([segue.identifier isEqualToString@"segueName"]){
//I use this if statement to check which segue I am performing, if I have multiple
//segues from a single view controller
FirstVc *newViewController = segue.destinationViewController;
newViewController.str = self.str;
}这将把字符串从你的主VC传递给你的新VC。这一切都假设您的转换是在界面构建器中进行布局的,或者是以某种方式允许您访问序列和标识符的
发布于 2012-12-26 22:05:55
在MainVC中获取一个属性,并从FirstVC中传递值,然后它将为您工作。
以下代码必须为MainVC格式
@property (strong, nonatomic) NSString *str;
@synthesize str;在FirstVC中
MainVC * vc = [[MainVC alloc]init];
vc.str = @"test";发布于 2012-12-26 22:22:38
Step1:
您必须创建一个viewcontroller类作为常量
第2步:
您可以在常量类中删除.m文件
Step3:
您必须在Projectname_Prefix.pch中导入xcode Constants.h资源,它将是其他资源文件夹中的的。现在常量类将充当全局类。
Step4:
现在,您可以在Constants.h.中声明变量在Constants.h中声明的变量将充当全局变量。你可以在任何地方访问它。
https://stackoverflow.com/questions/14041525
复制相似问题