この問題があります。手術があります。しかし、保存したい場合は?すべてを保存したい (テキストフィールド データ ピッカーとラベルの結果)
2 つのテキストフィールドと 1 つのデータピッカー + ラベルの結果があります。
これは私のMです:
#import "ViewController.h"
#import "ResultViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myLabel;
-(IBAction)calculate:(id)sender{
NSDate *past = _data.date ;
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
fromDate:past
toDate:now
options:0];
int z = [components day];
int a = ([_textField1.text intValue]);
int b = a*([_textField2.text intValue]);
int r = b * z / 20;
myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSDate *past = _data.date ;
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorianCalendar components:unitFlags
fromDate:past
toDate:now
options:0];
int z = [components day];
int a = ([_textField1.text intValue]);
int b = a*([_textField2.text intValue]);
int r = b * z / 20;
myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];
self.textField1.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key1"];
self.textField2.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key2"];
self.myLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key3"];
self.data = [[NSUserDefaults standardUserDefaults] objectForKey:@"key4"];
[[NSUserDefaults standardUserDefaults]synchronize];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"detail"]) {
ResultViewController* destVC = (ResultViewController*)segue.destinationViewController;
destVC.myString = self.myLabel.text;
}
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
[[NSUserDefaults standardUserDefaults] setObject:self.textField1.text forKey:@"key1"];
[[NSUserDefaults standardUserDefaults] setObject:self.textField2.text forKey:@"key2"];
[[NSUserDefaults standardUserDefaults] setObject:self.myLabel.text forKey:@"key3"];
[[NSUserDefaults standardUserDefaults] setObject:self.data forKey:@"key4"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
@end
このコードは正しいですか?
ありがとうございました....