为什么当应用程序在iOS模拟器中退出时,未调用viewWillDisappear或viewDidDisappear?

时间:2015-04-20 09:08:03

标签: ios objective-c xcode viewwillappear

我尝试在应用退出时调用 removeObserver()。但是当我使用 NSLog()进行检查时,我发现iOS模拟器中的app退出后,没有发现 viewWillDisappear() viewDidDisappear() 。我在类似的问题中使用单个视图模板,而不是导航控制器。

以下是源代码:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad() called");
    // Do any additional setup after loading the view, typically from a nib.

    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i = 0; i < 4; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = array[i];
        }
    }

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

}

- (void)viewWillDisappear:(BOOL)animated {
    NSLog(@"viewWillDisappear() called");
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = paths[0];
    return [documentDirectory stringByAppendingPathComponent:@"data.plist"];
}

- (void)applicationWillResignActive:(NSNotification *)notification {
    NSString *filePath = [self dataFilePath];
    NSArray *array = [self.lineFields valueForKey:@"text"];
    [array writeToFile:filePath atomically:YES];
}

@end

4 个答案:

答案 0 :(得分:2)

我建议您为应用程序更改添加侦听器:

- (void)registerNotifications {
    // Add observers to application state changes
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(applicationDidEnterBackground:)
                                                name:UIApplicationDidEnterBackgroundNotification
                                              object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(applicationDidBecomeActive:)
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];
}

答案 1 :(得分:1)

终止程序并返回Springboard后,Xcode不再关注iPhone模拟器的日志输出。除了输出不会转到Xcode的运行日志之外,所有功能仍然完全相同。

这就是您无法看到任何控制台输出的原因。

答案 2 :(得分:0)

我正在寻找iOS8及更高版本的解决方案,只需一个视图应用程序。

如果你设置:

应用程序不在后台运行。 NO

在Info.plist文件中。

然后将调用viewWillDisappear和viewDidDisappear。

没有它,他们就没有被召唤。

答案 3 :(得分:-1)

退出应用程序时,不会调用这两种方法。相反,将调用- (void)applicationWillTerminate:(UIApplication *)application中的方法AppDelegate.m。你可以用这种方法进行操作。