2013-03-27 37 views
0

我有一個UIButton,我以編程方式實現,因爲它僅在旋轉設備時需要。UIButton在返回到VC時沒有響應removeFromSuperView

當我旋轉時,按鈕需要消失,當我走到風景時出現。

只要我留在相同的ViewController我沒有問題。無論如何,我可以旋轉設備,按鈕出現並按預期消失。 該應用程序是基於TabController的應用程序,當我轉到另一個選項卡時,會發生相同的行爲。

這就是問題 當我回到原始視圖時,按鈕出現,但從未消失。它幾乎就像removeFromSuperView沒有被調用,但即使是這樣,按鈕也不會被刪除。

任何想法爲什麼這樣?

-(void)autoRotationDetection 
{ 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(orientationChanged:) 
    name:UIDeviceOrientationDidChangeNotification 
    object:[UIDevice currentDevice]]; 
} 

- (void) orientationChanged:(NSNotification *)note 
{ 
    UIDevice * device = note.object; 
    switch(device.orientation) 
    { 
     case UIDeviceOrientationPortrait: 
      /* start special animation */ 
      [_menuButton removeFromSuperview]; 
      break; 

     case UIDeviceOrientationPortraitUpsideDown: 
      /* start special animation */ 
      break; 

     default: 
      break; 
    }; 
} 

然後我打電話

-(void)viewWillAppear:(BOOL)animated 
{ 

    [self autoRotationDetection]; 
} 

對不起應該補充說。

+0

你應該顯示你調用removeFromSuperview的方法。 – rdelmar 2013-03-27 20:55:10

+0

我添加了代碼:-)對不起,我忘了:-) – 2013-03-27 21:06:45

+1

1.驗證你的方法正在調用,2.驗證'_menuButton'不是'nil'並指向正確的菜單按鈕。 3.確保你在主線程中(我認爲它應該是)。 – Joe 2013-03-27 21:06:46

回答

0

我看到了這種行爲(在轉到另一個選項卡並再次返回時,行爲會有所不同),因此在調用[super viewWillAppear]時會被忽略。嘗試添加,並看看它是否修復它。

後編輯:

我想更簡單的方法做,這是隻是看在viewWillLayoutSubviews的觀點,這就是所謂每有一個旋轉(和其他時間以及時間的界限,但對於這樣一個簡單的事情,這應該不重要)。在這個例子中,我隱藏或顯示而不是刪除,但這個概念應該適用於兩者。

-(void)viewWillLayoutSubviews { 
    BOOL portrait = self.view.bounds.size.height > self.view.bounds.size.width; 
    if (portrait) { 
     self.button.hidden = YES; 
    }else{ 
     self.button.hidden = NO; 
    } 
} 

你可以使通過保持畫像的價值的軌道上一次出現這種方法被稱爲這個更有效,且僅當肖像已經改變改變按鈕的狀態。

+0

我可能會把錯誤的地方,但它給了我一個錯誤:-) 我已經把它放在viewWillAppear方法 – 2013-03-27 21:11:04

+0

因此,你在問題中顯示的代碼是不是你實際上有什麼viewWillAppear? – rdelmar 2013-03-27 21:15:20

+0

@JeffKranenburg確保你添加了'[super viewWillAppear:animated]'而不僅僅是'[super viewWillAppear]'。 – rmaddy 2013-03-27 21:16:32