0

我在 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;

谢谢。

4

2 回答 2

0

创建一个重置方法,再次分配初始化视图控制器中的每个字符串。您可以将 textview 的文本设置为“”。

从理论上讲,您可以编写一个传递给对象(您的视图控制器)的辅助类,它使用自省来重置所有字符串或任何您想要的内容。这是模块化和可重用性的好方法,但是它需要 c 知识和自省。

于 2013-10-12T14:05:47.897 回答
0

是的,无论威廉姆斯说什么都是正确的。实现如下:

    -(IBAction)doClear:sender
         {
  [Yourtextview setString];
          }
于 2013-10-12T14:13:39.763 回答