2010-09-29 146 views
2

我根本無法讓UIDeviceOrientationDidChangeNotification着火。UIDeviceOrientationDidChangeNotification不會觸發?

我有我的AppDelegate,在這裏我想補充一個PolyOrientationViewController,其中有一個UInavigationController可以推VerticalNavigationControllerHorizontalViewController根據對iPad的方向。

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

    ISPolyOrientationTableViewController *pvc = [[ISPolyOrientationTableViewController alloc] init]; 
    [window addSubview:pvc.view]; 
    [pvc release]; 
    [window makeKeyAndVisible]; 

    return YES; 
} 

保利,垂直和水平的viewController實現:

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

PolyVC,這是最上面的視圖控制器,它有以下方法:

- (void)viewDidLoad { 

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

    [super viewDidLoad]; 
} 

它具有相關功能:

- (void)didRotate:(NSNotification *)notification { 
    //push the related controller 
} 

在PolyVC實例化後,調用didRotate:方法,從這裏開始,它簡單地停止生成UIDeviceOrientationDidChangeNotification,我想。

我在didRotate:方法中有一個斷點,我已經將應用程序加載到iPad上以確保它不是模擬器的東西,我做了一個新的xcode splitViewController項目,測試了旋轉工作,刪除了代碼並粘貼我已經檢查了方向鎖定按鈕,我試過執行-(BOOL) ShouldAutoRotate…我沒有被調用,我檢查了info.plist指定該應用程序支持所有方向,我嘗試了複製粘貼每一條通知代碼我找到了一個工作實例來清除拼寫錯誤。我有一些我可以做的,一些實現UIViewController,不使用IB,在ViewControllers中嵌套ViewControllers(PolyViewController擁有UINavigationController,擁有Vertical和Horizo​​ntalViewController)或任何其他的東西將使應用程序完全忽略interfaceOrientation通知?

我希望有人能指出我的錯誤:) 先謝謝您。

+0

找到這篇博客。這是非常有用的,簡單和工作http://blog.sallarp.com/iphone-shouldautorotatetointerfaceorientation-breaking-changes/ – 2011-04-19 17:38:54

回答

7

Extracted from the documentation,嘗試,如果它有助於:

你得到使用方向屬性的當前方位或爲UIDeviceOrientationDidChangeNotification通知註冊接收更改通知。在使用這兩種技術獲取方向數據之前,您必須使用beginGeneratingDeviceOrientationNotifications方法啓用數據傳遞。當您不再需要跟蹤設備方向時,請調用endGeneratingDeviceOrientationNotifications方法來禁用通知的傳遞。

6

此症狀(在初始化時會觸發一次通知,然後停止)聽起來像是因爲您已停用了iPhone中的方向更改。無論如何,這就是我發生的事情。