3
我在調用willMoveToWindow:
,我遇到了一個問題,它在視圖上被調用了兩次。willMoveToWindow被調用兩次
當一個新的視圖控制器被推到UINavigationController
,
willMoveToWindow:
被稱爲與nil
值現有的視圖(有道理的,因爲該視圖屏幕外移動)
在該時間之後,該方法willMoveToWindow:
被再次調用,但現在與原來的窗口。
我最初的想法是,交叉混合和調用window
財產之前的原始方法已經拉開。
要對我創建了一個小樣本項目,並確認相同的行爲在安全方面。
基本上我需要一種方法來確保視圖不在window
(因爲當視圖移動到實際上不應該運行的窗口時(至少不是兩次),我觸發邏輯)
作爲參考的問題可以用下面的代碼被複制:
@implementation RandomView
-(void)willMoveToWindow:(UIWindow *)newWindow {
// when the new view controller is pushed -
//the method is called twice on the existing view (on the screen view)-
//first time will be called with nil -
//second time with the original window
NSLog(@"********%s <RandomView %p> <Window %p>",__PRETTY_FUNCTION__,self,newWindow);
}
-(void)didMoveToWindow {
NSLog(@"********%s <RandomView %p> <Window %p>",__PRETTY_FUNCTION__,self,self.window);
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
RandomView *k = [[RandomView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:k];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
ViewController *vc = [[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
});
});
//[self becomeFirstResponder];
}
@end
EDIT 控制檯
[RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
[RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
//THIS IS THE ISSUE
[RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x0>
[RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x0>
[RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
[RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x7f8b21d220d0>
[RandomView willMoveToWindow:] <RandomView 0x7f8b21e16630> <Window 0x0>
[RandomView didMoveToWindow] <RandomView 0x7f8b21e16630> <Window 0x0>
你不能檢查'view.window'嗎?你爲什麼需要改頭換面?攪拌不好。 – Andy
那不相關 - 這只是一個附註。 問題是它被稱爲兩次 - –
它對我來說很有意義。 1. ViewDidLoad添加子視圖2.視圖添加到窗口。通過該命令鏈,您必須獲得兩次willMoveToWindow調用。您可能需要使用某個標誌或某物來註冊相關事件。 – Andy