2011-05-24 57 views
13

我正在嘗試檢測任何設備方向更改,以便我可以更新視圖。查看輪播通知:爲什麼didRotateFromInterfaceOrientation:沒有被調用?

我想要的方向是縱向還是橫向,以更新的觀點,讓我實現了這個方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations. 
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 

我知道,如果我想更新的意見正確顯示當前方位,我需要實現以下一些方法:現在

– willRotateToInterfaceOrientation:duration: 
– willAnimateRotationToInterfaceOrientation:duration: 
– didRotateFromInterfaceOrientation: 
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: 
– didAnimateFirstHalfOfRotationToInterfaceOrientation: 
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration: 

,問題是,我不明白爲什麼這些方法都沒有,當我旋轉模擬器會被解僱。

我試着以及這段代碼:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

但仍然一無所獲。所以我想知道,模擬器是否會觸發輪轉通知? 如果是,我做錯了什麼?

+0

你在哪裏實現了這個方法? – 2011-05-24 13:19:21

+0

是模擬器發送旋轉通知。 – Ishu 2011-05-24 13:20:55

+0

@Ishu:感謝您的信息; @Deepak:如果你指的是方法didRotateFromInterfaceOrientation,我在我的視圖控制器的.m文件中實現了它;如果您正在討論[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications],我在視圖控制器的viewDidLoad中執行了此操作。 – singingAtom 2011-05-24 13:31:08

回答

5

快速彙總,改變這種添加通知觀察者像

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,並添加方法:

[window addSubview:viewController.view]; 

這樣:

[window setRootViewController:viewController]; 

對不起,如果我把別人的時間,我的一個愚蠢的錯誤。我發現爲什麼方法– willRotateToInterfaceOrientation:duration:永遠不會被調用。有些東西將我的注意力引向了導航控制器:

只調用了根視圖控制器的willRotate方法。很可能你有一個奇怪的視圖控制器層次結構。

我在其他論壇發現這些帖子,我看了一下應用程序的代表。我的代碼如下:

CGRect bound = [[UIScreen mainScreen] bounds]; 
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)]; 
TestViewController *viewController = [[TestViewController alloc] init]; 
[window addSubview:viewController.view]; 
[viewController release]; 
[self.window makeKeyAndVisible]; 

問題是我沒有設置窗口的任何視圖控制器,但我只是添加一個視圖。由於急於測試某些東西,我做了一個錯誤。我必須修正這樣的代碼:

CGRect bound = [[UIScreen mainScreen] bounds]; 
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, bound.size.width, bound.size.height)]; 
TestViewController *viewController = [[TestViewController alloc] init]; 
[window setRootViewController:viewController]; 
[viewController release]; 
[self.window makeKeyAndVisible]; 
52

您需要

- (void) didRotate:(NSNotification *)notification 
{ 
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

     if (orientation == UIDeviceOrientationLandscapeLeft) 
     { 
     NSLog(@"Landscape Left!"); 
     } 
} 
+4

謝謝,這正是我所需要的 - 這似乎是[UIApplication sharedApplication]的一般建議。statusBarOrientation比UIDevice orientation更可靠,因此人們可能想要使用前者 - 我當然是。 – JosephH 2011-11-21 16:37:34

+0

你是對的@JosephH – Alkimake 2011-12-27 12:06:11

+0

請注意,在上面的代碼中,UIDeviceOrientationDidChangeNotification應該是一個常量,而不是實際的值。只是碰巧常量的值是UIDeviceOrientationDidChangeNotification :-) – 2012-09-07 05:07:17

相關問題