-3

これは昨日の質問のフォローアップです。私の .h は

@property (weak, nonatomic) IBOutlet UITextField *initialBudget;
@property (weak, nonatomic) IBOutlet UITextField *expenses;
@property (weak, nonatomic) IBOutlet UITextField *timeSpent;
@property (weak, nonatomic) IBOutlet UITextField *incomePerHour;

私の.mは:

- (IBAction)calculateResults:(id)sender {
    double budget = [initialBudget.text doubleValue ];
    double expense = [expenses.text doubleValue];
    double time = [timeSpent.text doubleValue];

    double hourlyIncome = (budget - expense)/time;
   incomePerHour.text = [NSString stringWithFormat:@"%.2f", hourlyIncome];
}

さまざまな方法を使用してテキストをViewControllerに保存しようとしましたが、アプリを閉じるとテキストが消えます。誰にもアイデアがありますか?私は夏の初めから Objective-C を勉強しているだけなので、潜在的な初心者の質問についてお詫び申し上げます。

4

4 に答える 4

1

あなたの目標は、incomePerHourテキスト フィールドのテキストを他のテキスト フィールドの計算結果で更新することのようです。

結果から文字列を作成し、テキスト フィールドの text プロパティを更新する必要があります。NSNumberFormatter通貨値の設定を実際に使用する必要があります。

double hourlyIncome = (budget - expense)/time;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *formattedString = [numberFormatter stringFromNumber:@(hourlyIncome)];
incomePerHour.text = formattedString;

更新- 実際の問題は、アプリの実行間で値を永続化することに関するものであるようです。この答えは、もはや適切ではありません。

于 2013-08-14T03:41:49.700 に答える
0

はい、テキストを保存する最も簡単な方法は、NSUSerDefault を使用することです。構造化データを保存する必要がある場合は、Sqlite を検討する必要があります。

于 2013-08-14T04:19:57.717 に答える
0

NSUserDefaulttextField の結果を保存するために使用できます。

次のようにしてみてください -

-(void)viewDidLoad
{
  //get and set value to your text field from the userdefault whenever you come from closeing the app..

  double result = [[NSUserDefault standardUserDefault]valueForKey:@"INCOME_VALUE"];
  incomePerHour.text = [NSString stringWithFormat:@"%.2f", result];
}

- (IBAction)calculateResults:(id)sender 
{
    double budget = [initialBudget.text doubleValue ];
    double expense = [expenses.text doubleValue];
    double time = [timeSpent.text doubleValue];

    double hourlyIncome = (budget - expense)/time;
   incomePerHour.text = [NSString stringWithFormat:@"%.2f", hourlyIncome];

   //set your result's value in the userdefault to access it later....
   [[NSUserDefault standardUserDefault]setDouble:hourlyIncome forKey:@"INCOME_VALUE"];
}

これが役立つことを願っています..

于 2013-08-14T04:00:53.760 に答える