你好!
我从Xcode开始(我是法国人,所以我的英语不是完美的抱歉),我需要你的帮助。
我的项目是制作一个应用程序,用于计算每个学校科目的平均课程(五)。我使用变量NSUserDefaults
来保存我的标记,但它不起作用,我不知道为什么。
这附加了我ViewController.h
为一所学科(其他都相同,只有变量变化)
#import <UIKit/UIKit.h>
@interface INF231ViewController : UIViewController{
IBOutlet UITextField *INF231CC1;
IBOutlet UITextField *INF231CC2;
IBOutlet UITextField *INF231ET1;
IBOutlet UITextField *INF231ET2;
IBOutlet UILabel *INF231moyenne;}
@property(nonatomic,retain) UITextField *INF231CC1;
@property(nonatomic,retain) UITextField *INF231CC2;
@property(nonatomic,retain) UITextField *INF231ET1;
@property(nonatomic,retain) UITextField *INF231ET2;
@property(nonatomic,retain) UILabel *INF231moyenne;
- (IBAction)INF231save:(id)sender;
- (IBAction)INF231calculer:(id)sender;
@end
这是ViewController.m:
#import "INF231ViewController.h"
@interface INF231ViewController ()
@end
@implementation INF231ViewController
- (void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int INF231noteCC1 = [defaults integerForKey:@"INF231noteCC1"];
int INF231noteCC2 = [defaults integerForKey:@"INF231noteCC2"];
int INF231noteET1 = [defaults integerForKey:@"INF231noteET1"];
int INF231noteET2 = [defaults integerForKey:@"INF231noteET2"];
NSString *INF231noteCC1String = [NSString stringWithFormat:@"%i",INF231noteCC1];
NSString *INF231noteCC2String = [NSString stringWithFormat:@"%i",INF231noteCC2];
NSString *INF231noteET1String = [NSString stringWithFormat:@"%i",INF231noteET1];
NSString *INF231noteET2String = [NSString stringWithFormat:@"%i",INF231noteET2];
INF231CC1.text = INF231noteCC1String;
INF231CC2.text = INF231noteCC2String;
INF231ET1.text = INF231noteET1String;
INF231ET2.text = INF231noteET2String;
[super viewDidLoad];}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];}
- (IBAction)INF231save:(id)sender {
[INF231CC1 resignFirstResponder];
[INF231CC2 resignFirstResponder];
[INF231ET1 resignFirstResponder];
[INF231ET2 resignFirstResponder];
int INF231noteCC1 = [[INF231CC1 text] integerValue];
int INF231noteCC2 = [[INF231CC2 text] integerValue];
int INF231noteET1 = [[INF231ET1 text] integerValue];
int INF231noteET2 = [[INF231ET2 text] integerValue];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:INF231noteCC1 forKey:@"INF231noteCC1"];
[defaults setInteger:INF231noteCC2 forKey:@"INF231noteCC2"];
[defaults setInteger:INF231noteET1 forKey:@"INF231noteET1"];
[defaults setInteger:INF231noteET2 forKey:@"INF231noteET2"];
[defaults synchronize];
NSLog(@"Data saved");}
@end
当我按下“保存”按钮时,它会在控制台“保存数据”中写入,但键盘未被禁用且数据消失。
我还提供了Main.Storyboard
的图片,以便您看得更清楚1。如果您需要其他一切来找出问题所在并帮助我解决问题,请不要犹豫。
我也想知道是否应该使用不同的变量
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
“* defaults”的虽然我有其他数据要保存在我的其他学科的其他ViewController
。
如果你花时间帮助我,非常感谢!
Xcode版本:7.3.1
iOS版本:9.3.3
答案 0 :(得分:0)
int
和NSInteger
之间存在差异。
[[INF231CC1 text] integerValue]
会返回NSInteger
,但您将其保存在int
。
这应该起作用:
NSInteger INF231noteCC1 = [[INF231CC1 text] integerValue];
同样适用于NSUserDefaults
:
integerForKey
会返回NSInteger
而非int
。
答案 1 :(得分:0)
请试试这个:
@implementation INF231ViewController
@synthesize INF231CC1, INF231CC2, INF231ET1, INF231ET2; // add
@end
希望对你有所帮助。