3

さて、私は現在、ナビゲーションコントローラー、詳細ビュー、およびモーダルビューを備えたテーブルビューを持つマスター/詳細アプリケーションを持っています。ナビゲーション ビューには飲み物の名前が表示され、詳細ビューには選択した飲み物の詳細が表示され、モーダル ビューでは新しい飲み物を作成したり、既存の飲み物を編集したりできます。すべては、私がすでにドキュメント ディレクトリに移動した plist から取り込まれ、すべてがそこから正常に読み取られています。ビューが空白に変わるかどうかを確認するために空の plist をテストしたので、これが機能することはわかっています。私の見解はすべて完璧に機能します。飲み物の編集と削除を追加し、テーブル ビューを新しい飲み物、削除した飲み物、または変更した飲み物で更新できます。ただし、plist ファイルに正常に書き込みません (masterViewController にある「drinks」という名前の配列から plist を更新しています)。

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [searchPaths objectAtIndex:0];
    NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"];

    [self.drinks writeToFile:writeableDBPath atomically:YES];
}

私は 2 つの NSLogs() を配置してこれをテストしまし。ログは、AppDelegate.m のログからコンソールにのみ表示されます。最後に、私の MasterViewController にある自分の Drinks 配列に AppDelegate.m ファイルからアクセスする方法がわからないので、appDelegate.m にある applicationWillTerminate メソッドを使用して plist に保存できます。ドリンク プロパティが MasterViewController にあるため、エラーをスローする appDelegate.m コードを以下に投稿します。

AppDelegate.m (メソッド)

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [searchPaths objectAtIndex:0];
    NSString *writeableDBPath = [documentsDirectory stringByAppendingPathComponent:@"DrinkDirections.plist"];

    [self.drinks writeToFile:writeableDBPath atomically:YES]; //THIS LINE THROWS AN ERROR BECAUSE PROPERTY DRINKS IS ONLY ON MY MASTERVIEWCONTROLLER NOT MY AppDelegate

}
4

3 に答える 3

4

まず、ここで他の人が述べたように、applicationDidEnterBackground:おそらくこれを行うのに適した場所です。第 2 に、View Controller はこの呼び出しをすべて単独で取得するわけではありません。これはUIApplicationDelegateプロトコル呼び出しであり、View Controller はその 1 つではありません。

やりたいことは、View Controller にUIApplicationDidEnterBackgroundNotification通知を受信させることです。 UIApplicationDelegate リファレンスには次のように書かれています。

また、アプリケーションは、このメソッドを呼び出すのとほぼ同時に UIApplicationDidEnterBackgroundNotification 通知を送信して、関心のあるオブジェクトに遷移に応答する機会を与えます。

したがって、View Controller で次のようなことを行いたいとします (警告: このコードを Web ページに直接入力しましたが、コンパイルしようとしませんでした):

- (void) viewDidLoad
{
    [super viewDidLoad];

    // Register for notification.
    [[NSNotificationCenter defaultCenter] addObserver:self 
                          selector:@selector(applicationDidEnterBackground:) 
                              name:UIApplicationDidEnterBackgroundNotification
                            object:nil];
}

- (void) viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
}

- (void) applicationDidEnterBackground:(NSNotification*)notification 
{
    // Do your plist saving here.
}
于 2012-06-27T03:30:52.060 に答える
0

「バックグラウンド実行をサポートするアプリケーションの場合、アプリケーションがバックグラウンドに移動するだけなので、ユーザーがアプリケーションを終了するときにこのメソッドは通常呼び出されません。」

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html

つまり、アプリケーションが「終了」することはありませんが、必要に応じてバックグラウンドに置かれ、バックグラウンドで強制終了されます。

アプリはデフォルトでバックグラウンドを「サポート」しているため、以下を検討する必要があります。

- (void)applicationDidEnterBackground:(UIApplication *)application
于 2012-06-26T23:55:59.757 に答える
0

applicationDidEnterBackgroundが呼び出されたときに .plist を保存することもできます。ここのアップルのドキュメントによると、

iOS 4.0 以降では、バックグラウンド実行をサポートするアプリケーションをユーザーが終了すると、applicationWillTerminate: メソッドの代わりにこのメソッドが呼び出されます。このメソッドを使用して、共有リソースを解放し、ユーザー データを保存し、タイマーを無効にし、後で終了した場合にアプリケーションを現在の状態に復元するのに十分なアプリケーション状態情報を保存する必要があります。また、アプリケーションのユーザー インターフェイスの更新を無効にし、一部の種類の共有システム リソース (ユーザーの連絡先データベースなど) を使用しないようにする必要があります。また、バックグラウンドで OpenGL ES を使用しないようにすることも不可欠です。

関連する部分を強調しました。お役に立てれば!

于 2012-06-27T00:00:12.497 に答える