0

每当用户进入后台时,我想在文本字段中保存一些文本我认为我写的所有内容都正确,因为我遵循了许多问题/答案。但是,当我关闭我的应用程序时,我的应用程序不会保存文本,甚至不会创建 plist,因此当我重新打开它时,文本字段是空的。这是代码:RootViewController.h:

@interface RootViewController: UIViewController <UITextFieldDelegate> {
UITextField *textField1;
UITextField *textField2;
UITextField *textField3;
UITextField *textField4;
}
@property (nonatomic, retain) UITextField *textField1, *textField2, *textField3, *textField4;
@end

RootViewController.m:

#import "RootViewController.h"
@implementation RootViewController
@synthesize textField1, textField2, textField3, textField4;
...
- (UILabel*)addNewLabel:(NSString*)_text
{
//Initializing TextViews
}
....
- (NSString *) saveFilePathB
{
NSString *path = @"/Applications/AppDissassembler.app/Cache/savefileb.plist";
return path;
}
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor whiteColor];
//creating textfields
self.textField1 = [self addNewTextfield:60:80:175:true:@"Binary Name Here":1];
self.textField2 = [self addNewTextfield:60:145:200:true:@"Offset Here (0xOffset)":2];
self.textField3 = [self addNewTextfield:75:210:175:false:nil:3];
self.textField4 = [self addNewTextfield:75:310:175:false:nil:4];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects:self.textField1.text,self.textField2.text,self.textField3.text,self.textField4.text,nil];
NSFileHandle *fout1;
[[NSFileManager defaultManager] createFileAtPath:[self saveFilePathB] contents:nil attributes:nil];
//open output file for writing
fout1 = [NSFileHandle fileHandleForWritingAtPath:[self saveFilePathB]]; 
[values writeToFile:[self saveFilePathB] atomically:YES];
[values release];

}
- (void)viewDidLoad 
{ 
NSString *myPath = [self saveFilePathB];

BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

if (fileExists)
{

    NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
    self.textField1.text = [values objectAtIndex:0];
    self.textField2.text = [values objectAtIndex:1];
    self.textField3.text = [values objectAtIndex:2];
    self.textField4.text = [values objectAtIndex:3];
    [values release];
}

UIApplication *myApp = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:@"UIApplicationDidEnterBackgroundNotification" object:myApp];
 [super viewDidLoad]; 
} 
- (void)dealloc {
[textField1 release];
[textField2 release];
[textField3 release];
[textField4 release];
[super dealloc];
}
@end

如果我改变这个:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:@"UIApplicationDidEnterBackgroundNotification" object:myApp];

对此:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:myApp];

我收到一条错误消息,指出未声明 UIApplicationDidEnterBackgroundNotification。

4

2 回答 2

0

您尝试的第二个版本UIApplicationDidEnterBackgroundNotification是标识符而不是文字字符串,是使用此功能的正确方法。它应该在 UIApplication.h 中定义。

你说你是在 iPhone 本身上编译的,所以听起来你那里的开发环境缺少这个定义。我自己没试过,所以不能确定。

一旦你克服了编译错误,我建议使用 NSLog 来查看你的代码到哪里,因为这比查找要创建的文件更容易。

于 2012-11-03T17:10:17.957 回答
0

它不会在您尝试的方法中运行,因此在 UIApplicationDidEnterBackground 委托中。您所要做的就是在使用 beginBackgroundTaskWithExpirationHandler 进入后台时处理您的进程。请参阅此主题:主题

于 2012-11-02T20:10:48.413 回答