【发布时间】:2014-06-03 07:53:12
【问题描述】:
当我从我家转到另一个家时,我会在文本字段中输入数据。我在顶部有一个导航栏,如果我点击后退按钮转到主页,我会丢失所有输入的数据。将数据保存在这些字段中的有效方法是什么?
谢谢。
编辑: 也许这会有所帮助,但是当我加载屏幕时,它会调用该方法-
- (void)viewDidLoad {
NSLog(@"Testing viewDidLoad");
[super viewDidLoad];
// Do any additional setup after loading the view.
_YourName.delegate = self;
_Notes.delegate = self;
// Set up the scroll view.
_scroller.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];}
但是当我返回上一个视图控制器或点击 ipad 上的主页按钮时,它不会调用:
- (void)viewDidUnload{
NSLog(@"Testing viewDidUnload");
[self setYourName:nil];
[self setPartnersName:nil];
[self setStartTime:nil];
[self setEndTime:nil];
[self setCurrentTemp:nil];
[self setCurrentWeather:nil];
[self setProjectName:nil];
[self setInstructorName:nil];
[self setClassNum:nil];
[self setWaypoints:nil];
[self setNotes:nil];
[super viewDidUnload];}
如果我点击主页按钮然后重新打开应用程序,数据也会保留在 UITextFields 和 UITextViews 中。如果我继续回到以前的 UIViewController,它就会消失。
- (IBAction)saveCurrent:(id)sender {
if([self checkFields]){
[self getEndingTime]; //gets the time when saved
// save all data to string using csv formatting
NSString *resultLine = [self getCSVformat];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// get today's date for file name ****DO DATE FORMATTER ONLY ONCE
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *todayDate = [dateFormatter stringFromDate:today];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/%@_%@_%@.csv",documentsDirectory,
todayDate,[[self ProjectName] text],[[self YourName] text]];
//save content to the documents directory
[resultLine writeToFile:fileName
atomically:NO
encoding:NSUTF8StringEncoding
error:nil];
}}
- (IBAction)retractKeyboard:(id)sender {
[self resignFirstResponder];}
【问题讨论】:
-
您的意思是您在视图控制器 1 中。您推送视图控制器 2,输入一些数据,然后回击视图控制器 1,然后如果您推送视图控制器 2,则您输入的数据丢失?
-
您能否更新问题以显示您编写的代码以在视图控制器 2 将被移除和再次呈现时保存数据?
-
我认为这就是我所缺少的,我不知道如何实现它。现在我只有它,所以它将数据保存到 csv 文件。我可以证明这一点。我只是不知道在离开 segue 时如何将数据保存在那里。
标签: ios uiviewcontroller uinavigationbar