我在xcode中有一个例程,它调用一个函数并将结果吐出到iPhone应用程序上的UITextView元素。
在例程启动之前,我想重置所有NSString对象/值以及我用来计算结果的任何变量。有快速的方法吗?
到目前为止,无论我在UITextField中重新输入什么,它都会在初始计算后被忽略。
求解按钮的代码: -
- (IBAction)Solve:(id)sender {
//
// Reset strings and variable code here
//
[_Number1 resignFirstResponder];
[_Number2 resignFirstResponder];
[_Number3 resignFirstResponder];
[_Number4 resignFirstResponder];
[_Number5 resignFirstResponder];
[_Number6 resignFirstResponder];
[_TargetNum resignFirstResponder];
// Dismiss Keyboard
int numb1,numb2,numb3,numb4,numb5,numb6,numTar;
numb1 = [self.Number1.text intValue];
numb2 = [self.Number2.text intValue];
numb3 = [self.Number3.text intValue];
numb4 = [self.Number4.text intValue];
numb5 = [self.Number5.text intValue];
numb6 = [self.Number6.text intValue];
numTar = [self.TargetNum.text intValue];
mainLoopOne(numb1,numb2,numb3,numb4,numb5,numb6,numTar);
// Start calculation with field values
readAnswers = @"Please read answers bottom-up.\n";
cantCalc = NULL;
finalResult = @"";
if (numb1 != 0) {
int ii;
for(ii = 0; ii < 6 ; ii++) {
if (allAnswers[ii] != NULL) {
finalResult = [finalResult stringByAppendingFormat:@"%@", allAnswers[ii]];
}
}
if (finalResult == NULL) {
cantCalc = @"Unfortunately, that doesn't seem to be possible, as there was no answer calculated.\n";
readAnswers = @"";
}
CompileText = [NSString stringWithFormat:@"%@\n%@\nfrom %d combination tries.", readAnswers, finalResult, countCombi];
[self.TextWin setText:CompileText];
}
countCombi = 0;
}
@end
大多数字符串和变量都设置在#import“ViewController.h”下面,因此它们是全局的: -
NSString *readAnswers;
NSString *cantCalc;
NSString *finalResult;
NSString *allAnswers[10];
NSString *CompileText;
NSString *readAnswers;
NSString *finalResult;
#define DIV 0
#define MUL 1
#define ADD 2
#define SUB 3
int n1,n2,n3,n4,n5,n6;
int answer_counter = 0;
int tar2 = 0;
int number[6];
int target = 0;
int used[6];
int countCombi;
感谢。
答案 0 :(得分:0)
创建一个重置方法,再次为视图控制器中的每个字符串分配。您可以将textview的文本设置为“”。
理论上你可以编写一个传递给对象(你的视图控制器)的辅助类,它使用内省来重置一个字符串或任何你想要的东西。这是模块化和可重用性的好方法,但它需要知识c和内省。
答案 1 :(得分:0)
是的,威廉姆斯说这是正确的。实施如下:
-(IBAction)doClear:sender
{
[Yourtextview setString];
}