2012-11-04 33 views
2

我有一個應用程序,它必須同時在縱向和橫向上工作,並且UITabBar應該調整到當前方向(它具有自定義背景和選定項目)。所以,對於其餘的意見,我只是覆蓋- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation方法,它完美的作品。在UITabBarController中爲「更多」UINavigationController處理設備旋轉。更多導航控制器

我該怎麼做.moreNavigationControllerUITabBarController?我嘗試添加一個觀察者(選擇是的UITabBarController的擴展名):

[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(didRotate:) 
name:UIApplicationDidChangeStatusBarOrientationNotification 
object:self.moreNavigationController]; 

但它永遠不會被調用。 我是否錯過了什麼或處理這種情況的最佳方法?

解決方案:爲什麼UIDeviceOrientation不能正確觸發,所以最好使用statusBarOrientation,作爲一種魅力。

最終代碼的工作是這樣的: 主UITabBarControllerviewDidLoad

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

didRotate選擇方法:

- (void) didRotate:(NSNotification *)notification{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if(UIInterfaceOrientationIsPortrait(orientation)) { 
     // Portrait 
    } else { 
     // Landscape 
    } 
} 

感謝您的幫助。

回答

2

正在註冊您的UITabBarController的通知永遠不會發布。在文檔NSNotificationCenter Reference看看爲addObserver:selector:name:object方法

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender 

notificationSender 的對象,其通知觀察者要接收;也就是說,只有該發件人發送的通知纔會發送給觀察者。 如果您通過零,通知中心不會使用通知的發件人來決定是否將其發送給觀察者。

因此,如果您指定. moreNavigationController作爲發件人,您將不會收到這些通知,因爲它從不發佈這樣的通知。相反,通過nil可以忽略發件人並收聽狀態欄更改,無論發送者是誰。

順便說一句,在這個SO Answer是你如何能夠對方向變化做出反應的總結。

最後。如果還是不行,你可以試試這個:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

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

請檢查我的最終解決方案以獲取正確答案。你的東西讓我變得可以解決問題,所以賞金就是你的。 :) 謝謝。 – whiteagle

+0

@whiteagle使用設備方向通知時,您可以獲取如下的方向:[[UIDevice currentDevice]方向]。也許這就是爲什麼它不適合你。否則你的解決方案看起來不錯很高興成爲幫助:) – iska

+0

設備方向甚至沒有被稱爲,donno爲什麼。 :/ – whiteagle

1

是的,你忘了張貼通知,這會叫你自己的通知:

[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:self]; 

,或者如果你不慣於只設置對象發送任何作爲零:

[[NSNotificationCenter defaultCenter] postNotificationName: UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 
+0

如何將帖子發我無法訪問控制器的通知? – whiteagle

+0

我剛剛編輯我的答案。 – edzio27

+0

'postNotificationName:'是手動觸發通知,如文檔所示: _使用給定名稱和發件人創建通知並將其發佈到接收者。_moreNavigationController是隻讀屬性,我不能用我自己的控制器覆蓋它,也不延伸任何實現。那麼,你建議從哪裏發佈通知呢?旋轉事件由系統生成,我正在尋找某種方式來處理它,而不是生成它 – whiteagle

0

的最好方法實現相同的是第一的addObserver然後取出觀察,以避免崩潰: -

-(void)viewDidLoad{ 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(didRotate:) 
               name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 
} 

//Now Remove Observer 
-(void)viewDidDisappear:(BOOL)animated 
{ 
[[NSNotificationCenter defaultCenter] removeObserver:self @"UIDeviceOrientationDidChangeNotification" object:nil]; 
} 
相關問題