我有一个文件(.m)计算按钮上的点击次数
counter=counter +1;
count.text = [NSString stringWithFormat:@"%i",counter];
在NSTimer之后会出现一个新屏幕(一个新的.m / .h / .xib文件),但我希望新屏幕上的标签中的分数(点击按钮)。
在第一个屏幕标题文件中,我正在执行:
IBOutlet UILabel *count;
但在NStimer倒计时之后我想在新屏幕(屏幕2)上显示得分
有谁知道我怎么能这样做?
需要更多信息?请问我!
答案 0 :(得分:0)
那么,你是如何初始化你的计时器的?我假设你正在使用像...这样的东西。
timerWithTimeInterval:target:selector:userInfo:repeats:
此方法采用一个选择器,在计时器到期/触发时调用。在此方法中,您应该显示视图,并且可以将计数传递给您正在创建的新视图。加载视图时,可以将计数设置为视图上的标签。
答案 1 :(得分:0)
只需给第二个视图控制器一个初始化程序,如:
- (id)initWithScoreText:(NSString*)scoreText {
if ((self = [super initWithNibName:@"MyNibName" bundle:nil])) {
scoreLabelText = [scoreText copy];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//set the text of the label
scoreLabel.text = scoreLabelText;
}
- (void) dealloc {
//release the string since we copied it
[scoreLabelText release];
[super dealloc];
}
然后在你的第一个视图控制器中,你会做类似的事情:
SecondViewController* newController = [[[SecondViewController alloc] initWithScoreText:count.text] autorelease];
[self.navigationController pushViewController:newController animated:YES];
编辑:根据Paul.s。的反馈更新。
答案 2 :(得分:0)
在randomMainVoid:action中,在推送第二个视图之前设置标签的属性。 我不知道你的secondViewController的名字以及你在secondViewController中命名的标签,所以我不能在这里给你准确的代码。
你能告诉我这些名字是什么吗?我会在这里为你发布代码。
您应始终使用大写字母命名您的ViewControllers,而不是得分。但在任何一种情况下:
Score *scoreView = [[Score alloc]initWithNibName:@"Score" bundle:nil];
[scoreView.count setText:count.text]; //Replace this with the text you want to pass
[self presentModalViewController:scoreView animated:YES];
或者如果您使用NavigationController,您可以将视图推送到堆栈:
[self.navigationController pushViewController:scoreView animated:YES];