我有这个问题。我有手术但是,如果我想保存?
我要保存所有内容(文本字段数据选择器并显示标签)

我有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


这个代码对吗?

谢谢....

最佳答案

夫妻的事。


在您的-viewDidLoad中,由于某种原因,您呼叫-synchronize。所有这些操作就是对用户默认值上的任何事件执行保存功能。据我所知,您没有任何权限,可以跳过此调用以进行-synchronize。
您的日志之一显示“ r”的值。该值是一个int值,您应该使用'%i'来正确显示它。您正在执行一些计算以获得该值,因此您可能希望该值是double或其他值,但当前正在将其强制转换为int。


我看不到任何有关用户默认设置的问题。因此,也许您可​​以指定确切的错误或未正确完成的操作以获得更好的帮助。

关于objective-c - NSUserDefaults standardUserDefaults,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13571294/

10-10 09:45