簡體   English   中英

為什么在 iOS 模擬器中應用程序退出時沒有調用 viewWillDisappear 或 viewDidDisappear?

[英]Why viewWillDisappear or viewDidDisappear not called when app quit in iOS simulator?

我正在嘗試在應用程序退出時調用removeObserver() 但是當我使用NSLog()查看時,發現在iOS模擬器退出后, viewWillDisappear()viewDidDisappear()都沒有被調用。 在類似的問題中,我使用的是單一視圖模板,而不是導航 controller。

這是源代碼:

#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

我建議您添加用於應用程序更改的偵聽器:

- (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];
}

在終止程序並返回Springboard之后,Xcode不再關注iPhone模擬器的日志輸出。 除了輸出不會進入Xcode的運行日志之外,其他所有功能仍然完全相同。

這就是為什么您看不到任何控制台輸出的原因。

我一直在尋找針對iOS8及更高版本的單視圖應用程序的解決方案。

如果您設置:

應用程序不在后台運行。 沒有

在您的Info.plist文件中。

然后,將調用viewWillDisappear和viewDidDisappear。

沒有它,他們就不會被召喚。

這些生命周期方法在將應用程序置於后台和前台時不起作用......我使用這些方法連接和斷開套接字所以最簡單的方法是在SceneDelegate中使用NotificationCenter ..

寫下一行

NotificationCenter.default.post(Notification(name: Notification.Name("appEnterForeground")))

sceneWillEnterForeground

 NotificationCenter.default.post(Notification(name: Notification.Name("appEnterBackground")))

sceneDidEnterBackground

viewDidLoad中寫入以下行

   NotificationCenter.default.addObserver(
        self,
        selector: #selector(myMethodonAppAcitve),
        name: Notification.Name(rawValue: "appEnterForeground"),
        object: nil
    )
    
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(myMethodonAppDeactive),
        name: Notification.Name(rawValue: "appEnterBackground"),
        object: nil
    )




@objc func myMethodonAppAcitve(){
        //do something when app is in forground
    }

@objc func myMethodonAppDeactive(){
        //do something when app is in background
    }

退出應用程序時,不會調用這兩種方法。 相反,將調用AppDelegate.m的方法- (void)applicationWillTerminate:(UIApplication *)application 您可以使用該方法進行操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM