我希望每当用户进入后台时在文本字段中保存一些文本我认为我正确地写了一切,因为我跟着很多问题/答案。但是,当我关闭我的应用程序时,我的应用程序不保存文本甚至创建一个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。
答案 0 :(得分:0)
它不会在您尝试的方法中运行,因此在UIApplicationDidEnterBackground委托中。您需要做的就是在使用beginBackgroundTaskWithExpirationHandler进入后台时处理您的进程。请参阅此主题:topic
答案 1 :(得分:0)
您尝试过的第二个版本,其中UIApplicationDidEnterBackgroundNotification
是标识符而不是文字字符串,是使用此功能的正确方法。它应该在UIApplication.h中定义。
你说你正在编译iPhone本身,所以听起来你所拥有的开发环境缺乏这个定义。我自己没试过,所以不能确定。
一旦超出编译错误,我建议使用NSLog查看代码的位置,因为这比查找要创建的文件更容易。