我們的APP從啟動到進入主頁面,是通過presentViewController構造了一個ViewController序列,類似於首頁 -> 登陸頁 -> 啟動加載頁 -> 主頁面
其中,在啟動加載頁的viewDidAppear方法裡做了很多邏輯處理:
-(void) viewDidAppear:(BOOL)animated{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ clientInfo = [YLSClientInfo new]; if([clientInfo needInit]){ [self mkdirAndDatabaseFile]; }else{ [self refreshVersion:[clientInfo currentVersion]]; } // 各種處理邏輯 }); }
UIViewController *origin = self.presentingViewController.presentingViewController; if([origin isMemberOfClass:[YLSLoginViewController class]]){ origin = self.presentingViewController.presentingViewController.presentingViewController; } [origin dismissViewControllerAnimated:NO completion:nil];
查了一下API,又上stackoverflow搜索了半天,似乎沒有辦法阻止這個默認行為。所以最後我的解決辦法是在中間的Controller上加了標記:
-(void) viewDidAppear:(BOOL)animated{ // 如果是由於調用了dismiss而觸發了此方法,不進行初始化 if(self.isDismissing){ return; } // 初始化加載邏輯 }
YLSBootstrapViewController *bootstrapController = (YLSBootstrapViewController*)self.presentingViewController; bootstrapController.isDismissing = YES; UIViewController *origin = self.presentingViewController.presentingViewController; if([origin isMemberOfClass:[YLSLoginViewController class]]){ origin = self.presentingViewController.presentingViewController.presentingViewController; } [origin dismissViewControllerAnimated:NO completion:nil];