2012-05-21 19 views
4

我很新的iOS和我拼命想獲得定位在我的應用更改工作。經過研究這裏就是我做什麼:不也是工作的iOS方向變化UIDeviceOrientationDidChangeNotification沒有收到

在我的應用我有一個NavigationController管理7個的UIViewController子類。

在項目摘要標籤我激活的所有4的設備取向。

每個UIViewController子類有廈門國際銀行文件,廈門國際銀行的所有文件已「自動調整大小子視圖」激活。

的子類的UIViewController都:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
      interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
      interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

他們也都有:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

和:

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

用的NSLog(...)語句(從來沒有實現過打印,調試器也不會輸入這些方法)。

我也嘗試使用:分別

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]; 
NSLog(@"will receive orientation notifications: %@", [email protected]"YES":@"NO"); 

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

-(void)orientationChanged: (NSNotification*) notification { NSLog(@"orientationChanged"); }

和。

當我在AppDelegate中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法做beginGeneratingDeviceOrientationNotifications等,orientationChanged:在啓動時被調用一次,但從來沒有一次但是我旋轉設備,當我這樣做是在UIViewController中的一個子類這是從來沒有叫!

到目前爲止,所有我想要實現越來越取向通知來旋轉的UIImageView和UIImage的(無需在不同方向的任何佈局的變化)。

UIDeviceOrientation o = [UIDevice currentDevice].orientation; 

總是返回UIDeviceOrientationPortrait

這可能是我錯過了在文檔或計算器的東西,但我顯然不能找出我需要做的/加得到它在我的設置工作。另外我對於stackoverflow很新,所以我希望我的帖子不違反任何平臺慣例。

任何幫助/提示,​​非常感謝。非常感謝!

編輯: getOrientationUpdates始終是YES,這對我來說顯得很陌生,因爲通知回調選擇器在旋轉時從未被調用!

編輯:在我的AppDelegate的didFinishLaunchingWithOptions我做:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.regScreenController = [[RegistrationScreenController alloc] initWithNibName:@"RegistrationScreenController" bundle:nil]; 

navCtrl = [[UINavigationController alloc]initWithRootViewController:self.regScreenController]; 

[navCtrl setNavigationBarHidden:YES]; 
self.window.rootViewController = self.regScreenController; 
[self.window addSubview:navCtrl.view]; 
[self.window makeKeyAndVisible]; 
return YES; 

回答

5

看看你是否在你的AppDelegate'sdidFinishLaunchingWithOptions設置self.window.rootViewController:方法,因爲如果你只添加子視圖窗口中的方向變更通知不開火。

+0

感謝您的回答!如果你看我的編輯,我設置rootViewController,我是否做錯了,也許涉及到UINavigationController?仍然堅持我缺少的東西...... – robscure

+0

或以不同的方式表述:如果問題與rootViewController相關,如果UINavigationController使用rootViewController進行初始化,並且將UINavigationController設置爲子視圖,那麼它是否也是正確的將UINavigationController的rootViewController設置爲窗口的rootViewController,或者在這裏連接東西以獲取方向更改通知的正確方法是什麼?非常感謝 – robscure

+0

我不這麼認爲,你不應該在窗口中添加子視圖。只需設置rootViewController,並且層次結構中的其他內容應該從rootViewController開始,即使它是UINavigationController,但絕對不會將子視圖添加到窗口中。 – graver

相關問題