Iphone 要将整数值传递给另一个视图吗

Iphone 要将整数值传递给另一个视图吗,iphone,xcode4,nsinteger,Iphone,Xcode4,Nsinteger,我正在创建一个iphone应用程序,其中我的oneviewcontroller中有一些整数值,我想在我的secondviewcontroller中有这个整数值 值将是随机的。 我将该整数值存储在id分数中 我正在使用这段代码,但我不能得到整数值 代码: 整数值在secondviewcontroller处始终变为零。 任何帮助都将不胜感激。 谢谢 我的代码: FirstView.h文件 NSInteger myScore; id score; NSInteger totalscore;

我正在创建一个iphone应用程序,其中我的oneviewcontroller中有一些整数值,我想在我的secondviewcontroller中有这个整数值

值将是随机的。 我将该整数值存储在id分数中

我正在使用这段代码,但我不能得到整数值

代码:

整数值在secondviewcontroller处始终变为零。
任何帮助都将不胜感激。
谢谢

我的代码:

FirstView.h文件

NSInteger myScore;  
id score;  
NSInteger totalscore;
 int scorevalue ;
.m文件

NSInteger *cal = (myScore * 100)/400  ;

        totalscore = cal;
        score = totalscore;
SecondView.h文件

NSInteger myScore;  
id score;  
NSInteger totalscore;
 int scorevalue ;
SecondView.m文件

FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
value = firstVC.Score;
NSLog(@"SCORE : %d" ,value );

因为'id'表示对象(),但我们需要数据类型

与其将其声明为
id
,不如将其声明为
int
本身

大概

int score;

编辑:

还要添加属性并合成它。 In.h

英寸

@synthesize score;
在第一个视图控制器中,通过将值传递给第二个视图

NSInteger *cal = (myScore * 100)/400  ;
totalscore = cal;
score = totalscore;
secondViewObj.score = score;

一种简单的方法是,将该值存储在NSUserDefault中,然后从secondviewcontroller中的NSUserDefault中获取整数的值。

在secondviewcontroller中的.h文件中获取该值

int value;
在SecondViewController中生成以下函数

-(void)setValue:(int)number{
    value=number;
}
现在,在第一视图中,控制器执行以下操作:

ParentViewController *objSecond = [[ParentViewController] initwithNibName:@"parentView.xib" bundle:nil];

[objSecond setValue:15]; // Pass actual Value here
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
现在,在secondViewController视图中,将出现方法写入

-(void)viewWillAppear:(BOOL)animated{
      myValue = value;
}
请检查我手写的拼写错误。希望这有帮助

如果您没有使用navigationContoller,那么可以执行类似的操作

SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
[objSecond setValue:15]; // Pass actual Value here
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];
例如:

如果我需要在FirstViewController中存储int值4

[[NSUserDefaults standardUserDefaults] setObject:4 forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];
我可以在SecondViewController中获取get,如下所示:

int value = [[NSUserDefaults standardUserDefaults] objectForKey:@"integer"]
NSLog(@"integer = %d", [[NSUserDefaults standardUserDefaults] objectForKey:@"integer"]);

希望它能很好地工作。

我尝试过这个,但不知道如何在我的第二个视图中获得该值。。这是我试图解决的主要问题..耶。。我试过了,但值仍然变为0(零),然后我尝试了开发人员的答案。。而且效果很好。。我得到了我的解决方案:)谢谢。。!!谢谢你的快速回复。。但我对iPhone的开发还不熟悉。那么,您能告诉我如何在NSUserDefault中存储值吗?你有任何链接或文档吗?下一个答案会发给你。@Sascha:我编辑了我的代码。。请看一看。@Shrey:好的。。我尝试过这个,但我想在SecondView中看到这个值。。在secondview中,值变为0(零)。。有什么帮助吗哦。。。是的,它很好用。。非常感谢开发人员:)过去两天我一直在做这个。。再次感谢您的帮助!!:)