我在通过导航控制器运行时保存外部变量时出现问题,这是一个应用程序,我在这里计算游戏点数我的代码用于计算点数:
这是用于计算积分的IBAction:
- (IBAction)RedTeloBallsTotal:(id)sender {
NSString *firstString = RedTeloBallsOne.text;
NSString *secondString = RedTeloBallsTwo.text;
NSString *thirdString = RedTeloBallsThree.text;
NSString *fourthString = RedTeloBallsFour.text;
NSString *LEGAL = @"0123456789";
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
NSString *filteredOne = [[firstString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
NSString *filteredTwo = [[secondString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
NSString *filteredThree = [[thirdString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
NSString *filteredFour = [[fourthString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
firstString = filteredOne;
secondString = filteredTwo;
thirdString = filteredThree;
fourthString = filteredFour;
//Here we are creating three doubles
double num1;
double num2;
double num3;
double num4;
double output;
//Here we are assigning the values
num1 = [firstString doubleValue];
num2 = [secondString doubleValue];
num3 = [thirdString doubleValue];
num4 = [fourthString doubleValue];
output = num2 + (num1 * 2) + num4 + (num3 * 25);
//Now we are going to display the output in the label.
RedTeloBallsTotal.text = [NSString stringWithFormat:@"%.0f",output];
}
答案 0 :(得分:0)
要保存计算值,您应该在对象中为它创建一个声明的属性。
例如,在此对象的标题中,将其与其他属性一起添加:
@property (nonatomic, assign) double redTeloBallsTotalOutput;
然后,在实现文件(.m文件)的顶部,您应该看到其他@synthesize行,添加以下内容:
@synthesize redTeloBallsTotalOutput;
要在上面的函数中设置值,请使用:
self.redTeloBallsTotalOutput = output;
最后,要在此对象中的其他位置使用它,请使用:
self.redTeloBallsTotalOutput