jjzjj

objective-c - applicationWillResignActive 通知未在设备上捕获

coder 2024-01-14 原文

我试图在应用程序关闭时将一个简单的字符串保存在一个简单的 .plist 文件中。

这是我的代码:

- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    self.kmOld.text = [array objectAtIndex:0];

    [array release];
}


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

然后按照applicationWillResignActive方法:

 - (void)applicationWillResignActive:(NSNotification *)notification {

    NSString *text = [[NSString alloc]initWithFormat:@"%@",kmNew.text];
    NSLog(@"%@",text);

    if ([text intValue]>0) {
        NSArray *array = [[NSArray alloc] initWithObjects:text, nil];

      BOOL success = [array writeToFile:[self dataFilePath] atomically:YES];
        NSLog(@"%d",success);
        [array release];
    }
    [text release];
}

这在模拟器上运行良好,但它似乎被设备忽略了...... 哪里有问题?谢谢...

最佳答案

看看http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/以获得 UIApplication 生命周期的良好概述。根据您的应用程序运行的 iOS 版本以及导致您的应用程序终止/后台运行的操作,您可能没有收听正确的通知,并且可能还需要观察 UIApplicationWillTerminateNotification。

关于objective-c - applicationWillResignActive 通知未在设备上捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7757025/

有关objective-c - applicationWillResignActive 通知未在设备上捕获的更多相关文章

随机推荐