【问题标题】:Want to pass integer value to the another view想要将整数值传递给另一个视图
【发布时间】:2011-07-27 10:51:58
【问题描述】:

我正在创建一个 iphone 应用程序,其中我的 oneviewcontroller 中有一些整数值,我希望在我的 secondviewcontroller 控制器中使用该整数值。

值将是随机的。 我将这个整数值存储在 id score

Nw 我正在使用此代码,但我无法获取整数值。

代码:passing integer value from one UIView to another UIview

整数值在第二个视图控制器处始终为零。
任何帮助将不胜感激。
谢谢。

我的密码:

FirstView.h 文件

NSInteger myScore;  
id score;  
NSInteger totalscore;

.m 文件

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

        totalscore = cal;
        score = totalscore;

SecondView.h 文件

 int scorevalue ;

SecondView.m 文件

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

【问题讨论】:

  • @Sascha : 我编辑了我的代码.. 请看一下..
  • @Shrey : 好的.. 我试过了,但我希望 SecondView 中的值.. 在 secondview 中,值变为 0(零).. 任何帮助..!!:o

标签: iphone xcode4 nsinteger


【解决方案1】:

例子:

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

[[NSUserDefaults standardUserDefaults] setObject:4 forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];

我可以在 SecondViewController 中获取,

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

希望它能完美运行。

【讨论】:

  • 哦...是的,它工作得很好..非常感谢开发人员.. :) 我从过去 2 天开始一​​直在努力.. 再次感谢您的帮助..!! :)
【解决方案2】:

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

【讨论】:

  • 感谢您的快速回复。但我是 iPhone 开发的新手。那么你能告诉我如何在 NSUserDefault 中存储值吗?你有任何链接或文件吗??
  • 将在下一个答案中发送给您。
【解决方案3】:

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

与其将其声明为id,不如将​​其声明为int

类似的,

int score;

NSInteger score;

编辑:

同时添加属性并合成它。 在.h

@property(nonatomic,assign) NSInteger score;

在.m

@synthesize score;

在第一个视图控制器中,将值传递给第二个视图,

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

【讨论】:

  • 我试过了,但不知道如何在我的 secondView 中获得该值。这是我要解决的主要问题。
  • 是的..我试过了,但值仍然变为 0(零)然后我尝试了开发人员的答案..它工作正常..我得到了我的解决方案 :) 谢谢..!!
【解决方案4】:

把这个放在 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 的 viewWillAppear 方法中写下这个。

-(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];

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多