Ios 保存UITextFields、UILabels和UITextView等字符串

Ios 保存UITextFields、UILabels和UITextView等字符串,ios,xcode,nsstring,Ios,Xcode,Nsstring,在通过导航控制器运行时,我在保存外部变量时遇到问题,这是一个我计算游戏点数的应用程序。这里是我计算点数的代码: - (IBAction)RedTeloBallsTotal:(id)sender { NSString *firstString = RedTeloBallsOne.text; NSString *secondString = RedTeloBallsTwo.text; NSString *thirdString = RedTeloBallsThree.text; NSString

在通过导航控制器运行时,我在保存外部变量时遇到问题,这是一个我计算游戏点数的应用程序。这里是我计算点数的代码:

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


}
这是用于计算点的iAction:

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


}

要保存计算值,应在对象中为其创建声明的属性

例如,在该对象的标题中,将其与其他属性一起添加:

@property (nonatomic, assign) double redTeloBallsTotalOutput;
然后,在实现文件.m文件的顶部,您应该看到其他@synthesis行,添加以下行:

@synthesize redTeloBallsTotalOutput;
要设置上述函数中的值,请使用:

self.redTeloBallsTotalOutput = output;
最后,要在此对象内的其他位置使用它,请使用:

self.redTeloBallsTotalOutput

我不知道如何将变量输出保存为外部变量,以便uitextfields中的任何文本都不会丢失,但不会影响计算这只是将计算输出保存到一个变量,您可以使用该变量进行任何操作。它不在文本字段中保存文本,只保存输出。如果要保存文本,则需要执行相同的操作,但需要创建NSString属性以将其存储在中。