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

あなたが試した 2 番目のバージョン (UIApplicationDidEnterBackgroundNotificationはリテラル文字列ではなく識別子) は、この機能を使用する正しい方法です。UIApplication.h で定義する必要があります。

あなたはiPhone自体でコンパイルしていると言うので、そこにある開発環境にはこの定義が欠けているようです。私はそれを自分で試していないので、確信が持てません。

コンパイル エラーを回避したら、作成するファイルを探すよりも簡単に、NSLog を使用してコードがどこに到達したかを確認することをお勧めします。

于 2012-11-03T17:10:17.957 に答える
0

試したメソッドでは実行されないため、UIApplicationDidEnterBackground デリゲートで実行します。あなたがしなければならないことは、beginBackgroundTaskWithExpirationHandler でバックグラウンドに移行しながらプロセスを処理することだけです。このトピックを参照してください:トピック

于 2012-11-02T20:10:48.413 に答える