2010-10-05 52 views
3

我的視圖控制器不響應didRotateFromInterfaceOrientation,儘管我已經添加在我的代碼如下:視圖控制器不響應didRotateFromInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    [self.popOver dismissPopoverAnimated:NO]; 
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
    ... My Custom Code... 
    } 
} 

我在這裏幹什麼什麼了嗎?

+0

有一件事我只注意到我的控制器類是NSObject的一個子類,不是UIViewController的子類。我無法改變這個層次結構。仍然有什麼辦法呢? – Abhinav 2010-10-05 03:51:34

回答

3

如果你不能從UIViewController的(這是不幸的)繼承,您可以使用this

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

然後註冊開始接收UIDeviceOrientationDidChangeNotification通知。

+0

優秀。謝謝。 – Abhinav 2010-10-05 04:38:25

2

如果您的UIViewController是某個根視圖中的子項,那麼IB在默認情況下不會將其作爲子控制器添加到根控制器。解決這一問題的最簡單的方法是修改您的根控制器:

- (void)viewDidLoad 
{ 
    [super viewDidLoad];  
    [self addChildViewController:(UIViewController*) self.yourChildController]; 
} 

這應該做的伎倆。現在你的孩子控制器將收到兩個:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; 

消息。

0

我認爲這裏的真正的答案(更準確的答案的鏈接的問題)是你需要調用

[super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 

在子類實現didRotateFromInterfaceOrientation方法。例如:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
    // Then your code... 
} 

這不是蘋果文檔中提到,但省略的時候造成了一些嚴重的和難以解釋的問題,對我來說...