2011-10-25 42 views
2

我的拆分視圖控制器代碼:shouldAutorotateToInterfaceOrientation被多次調用 - 這是正常的嗎?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    LeftViewController *hvc = [[[LeftViewController alloc] initWithNibName:nil bundle:nil] autorelease];  
    DetailViewController *dvc = [[[DetailViewController alloc] initWithNibName:nil bundle:nil] autorelease]; 
    UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:hvc] autorelease]; 
    UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:dvc] autorelease]; 
    UISplitViewController *svc= [[[UISplitViewController alloc] init] autorelease]; 
    [svc setViewControllers:[NSArray arrayWithObjects:rootNav, detailNav, nil]]; 
    svc.delegate = dvc; 
    [window setRootViewController:svc]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

DetailViewController.m和LeftViewController.m都包含

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    NSLog(@"should rotate asked to detailviewcontroller"); 
    return YES; 
} 

在iPad模擬器,我可以看到這些多次調用shouldAutorotateToInterfaceOrientation當應用程序只是獲取推出

should rotate asked to detailviewcontroller 
should rotate asked to leftviewcontroller 
should rotate asked to leftviewcontroller 
should rotate asked to detailviewcontroller 
... 
should rotate asked to leftviewcontroller // these two lines 
should rotate asked to detailviewcontroller // are repeated 13 times 
... 
should rotate asked to leftviewcontroller 
should rotate asked to detailviewcontroller 

背後的原因是什麼?我必須提到我不改變模擬器的方向

回答

1

shouldAutorotateToInterfaceOrientation是爲了檢查您的視圖是否支持特定的方向。

這並不一定意味着您的設備正在移動/正在旋轉。

您不應該擔心導致外部實體多次查詢您的視圖控制器的實現細節,而只是爲您的視圖返回適當的值。

如果你有興趣檢測設備旋轉,你可能會決定依靠UIDeviceOrientationDidChangeNotification

+1

據我所知,外部實體可以調用'shouldAutorotateToInterfaceOrientation'然而他們想多次。但超過15倍?觸發器是什麼?只是想到如果你看到一些明顯錯誤的地方,你們都要仔細檢查。 –

相關問題