How to detect the rotation change when coming back to tableView from detailView?
I have a UITableViewController and DetailViewController that pops up when concrete row is selected. The problem is the tableView does not call "didRotateFromInterfaceOrientation" method when rotation is did in DetailViewController. It's logical, however I need to reload a table if orientation has changed.
I'm trying this code but it doesn't work:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.interfaceOrientation != [UIDevice currentDevice].orientation){
[self handleTableViewRotation];
}
}
I'm assuming there should be some property that holds orientation of tableView that is independent of current device orientation property...
Maybe the most elegant solution is coding a protocol. Whenever your DetailViewController
enters in a rotation callback, call reloadData
method of your tableView.
Or... you can continue that way. In that case you have to store the tableViewController's last orientation as a instance var and compare it to the current. After that, remember setting the new orientation
EDIT: First declare a new Protocol.
@protocol RotationDelegate
- (void) didRotate;
@end
In your DetailViewController.h:
#import RotationDelegate.h
@interface DetailViewController : UIViewController {
id <RotationDelegate> rotationDelegate;
}
@property (nonatomic, assign) id <RotationDelegate> rotationDelegate;
In DetailViewController.m:
@synthesize rotationDelegate;
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[rotationDelegate didRotate];
}
Now your ViewController that contains the tableView:
@interface RootViewController : UITableViewController <RotationDelegate> {
...
}
And finally implement didRotate
method and call [yourTableView reloadData]
inside. Remember that you have to set the rotation delegate after init your DetailViewController.
精彩评论