1

ContentController という名前のクラスがあります。そのインスタンスを ViewController から作成します。最初のフェローは、リモート サーバーからデータを取得し、それに対して何かを行います。次に、情報を ViewController に渡し、ユーザーに何か良いことを示します。ここまでは順調ですね。

さて、問題は AppDelegate の使用中です。アプリケーションがバックグラウンド モードに入ろうとするときに、(ContentController の) 同じインスタンスにアクセスしたい。また、デバイスにいくつかの属性を保存します。そして、これは機能していません。

助けてくれませんか?

4

3 に答える 3

2

本当に AppDelegate から ContentController インスタンスにアクセスしたい場合は、AppDelegate でプロパティを作成できます。

//AppDelegate.h
@property (strong, nonatomic) ContentController *contentController;

ViewController で使用する必要がある場合は、

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.contentController = [[ContentController alloc] init];

または、AppDelegate インスタンスを指す ViewController クラスにプロパティを作成することもできます。

self.contentController = appDelegate.contentController;
于 2012-05-02T09:19:43.033 に答える
1

ContentControllerからの通知を登録します。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:)     name:UIApplicationWillResignActiveNotification object:nil];

そして、applicationWillResignActive:Content Controller内にメソッドを実装して、やりたいことを実行します。

- (void)applicationWillResignActive:(NSNotification *)notification
{
   // Your server calls
}

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Notifications/Articles/Registering.html#//apple_ref/doc/uid/20000723-98481-BABHDIGJ

于 2012-05-01T09:01:31.087 に答える
0

プロパティ値を applicationDidEnterBackground に保存するには: 次のように、クラスの viewDidLoad メソッドに通知オブザーバーを追加できます。

UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

viewDidUnload では、これを追加する必要があります:

[[NSNotificationCenter defaultCenter] removeObserver:self];

また、これら 2 つのメソッドをクラス .m-file に追加します (以下に、私のアプリケーションの 1 つからの例を示します)。

- (void)applicationWillResignActive:(NSNotification *)notification {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [array addObject:[NSNumber numberWithInt:self.event.eventID]];
        [array addObject:self.event.eventName];
        [array addObject:[NSNumber numberWithDouble:[self.event.ticketPrice doubleValue]]];

        [array writeToFile:[self dataFilePath] atomically:YES];
        [array release];
    }

そして、このメソッドはファイルからデータをロードし、viewDidLoad で呼び出します。

- (void)loadEventFromEventPlist {
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        self.event.eventID = [[array objectAtIndex:0] intValue]; 
        self.event.eventName = [array objectAtIndex:1];
        self.event.ticketPrice = [NSNumber numberWithDouble:[[array objectAtIndex:2] doubleValue]];
        [array release];
    }
}

ファイル名を取得するには、このメソッドが必要です。

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
于 2012-05-02T09:00:23.150 に答える