2011-09-16 29 views
0

我有一個UITableViewController和DetailViewController彈出時,具體行被選中。問題是在DetailViewController中執行旋轉時,tableView不會調用「didRotateFromInterfaceOrientation」方法。這是合乎邏輯的,但是如果方向改變了,我需要重新加載表格。如何檢測從detailView返回到tableView時的旋轉變化?

我想這個代碼,但它不工作:

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    if (self.interfaceOrientation != [UIDevice currentDevice].orientation){ 
    [self handleTableViewRotation]; 
    } 
} 

我猜想應該有一些財產持有的tableView的定位是獨立當前設備取向財產...

+0

只是爲了澄清,視圖控制器已做了輪換,只是通知方法(willRotate,didRotate)沒有被調用? – jrturton

+0

是的。實際上,這些通知方法沒有被調用是合乎邏輯的,因爲實際的旋轉發生在TBC之外 - 在DetailViewController中。所以,我正在尋求解決這個問題,並且我已經結束了使用@ender提供的第二種解決方案。我在TBC中創建了一個存儲tableViewController最後一個方向的屬性,並且我使用viewWillAppear方法將它與TBC中的當前設備方向進行了比較。 – Centurion

回答

2

也許最優雅的解決方案是編碼一個協議。無論何時您的DetailViewController進入循環回調,請致電reloadData您的tableView的方法。

或...你可以繼續這種方式。在這種情況下,您必須將tableViewController的最後方向作爲實例var存儲並將其與當前值進行比較。之後,記得設置新的方向

編輯:首先宣佈一個新的協議。

@protocol RotationDelegate 

    - (void) didRotate; 

@end 

在你DetailViewController.h:

#import RotationDelegate.h 

@interface DetailViewController : UIViewController { 
    id <RotationDelegate> rotationDelegate; 
} 

@property (nonatomic, assign) id <RotationDelegate> rotationDelegate; 

在DetailViewController.m:

@synthesize rotationDelegate; 


-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 

    [rotationDelegate didRotate]; 

} 

現在您的ViewController包含的tableView:

@interface RootViewController : UITableViewController <RotationDelegate> { 
    ... 
} 

最後落實didRotate方法和呼叫[yourTableView reloadData]裏面。記住你必須在初始化你的DetailViewController之後設置旋轉委託。

+0

我對協議很不熟悉。我這樣做,我有時需要包括委託協議和實現方法,但我不知道如何實現您建議的優雅解決方案。你能提供更多細節嗎? – Centurion

+0

我已經添加了一些代碼,希望這有助於 – ender

+0

感謝@ender :)它看起來像一個協議和旋轉委託解決方案類似於具有純屬性(不使用協議)像detailViewController上的「tableViewController」和分配它tableViewController的值。我想這將是一個懶惰的解決方案:)它看起來像實現一個協議是一些工作約定這樣的任務。但是非常感謝你清除這個;) – Centurion