什么对象是dipatching的UIViewController旋转的方法调用,即负责:
- shouldAutorotateToInterfaceOrientation:
- willRotateToInterfaceOrientation:时间:
- willAnimateFirstHalfOfRotationToInterfaceOrientation:时间:
- willAnimateSecondHalfOfRotationFromInterfaceOrientation:时间:
- didRotateFromInterfaceOrientation:
我猜想,UIApplication(但也许是AppDelegate或UIWindow)。
看来,UIApplication是派遣一个信息,活动视图控制器。
但如何做你的视图控制器实例得到这些信息?
该邮件被转发到的第一个视图控制器的观点已被添加到UIWindow实例。
这可以归结为3个基本情况:
-
-
他们的观点是的ViewController
直接添加到UIWindow
(例如单一视图的应用程序)
-
-
-
在导航控制器
导航的应用程序,则
导航控制器转发
信息的积极的意见看法
控制器。
-
-
-
在一个标签栏标签栏控制器
基于应用程序,那么标签栏
控制器转发邮件
活动的意见看法控制器(或
积极导航控制器)。
-
您将有问题,如果你是建立一个与多个视图的应用程序,但不使用导航控制器或一个标签栏控制器。 如果您在交换意见,并出UIWindow实例手动,您将不会收到这些消息可靠。
首先重写UIViewController方法:
Java代码
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
你也可以根据toInterfaceOrientation的不同值来判断是否允许旋转。这个传入参数有四种取值:
Java代码
UIInterfaceOrientationLandscapeLeft 横向Home键在左
UIInterfaceOrientationLandscapeRight 横向Home键在右
UIInterfaceOrientationPortrait 正常
UIInterfaceOrientationPortraitUpsideDown 反向Home键在上
可以在下面的方法中处理旋转后要重画的组件,或者重载另一个NIB文件。
Java代码
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// 重新加载一个Nib文件
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[NSBundle mainBundle] loadNibNamed:@"LoginViewLandscape" owner:self options:nil];
}else {
[[NSBundle mainBundle] loadNibNamed:@"LoginView" owner:self options:nil];
}
// 重写Toolbar
// Set Toolbar
UIBarButtonItem *newChat = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:UIBarButtonItemStylePlain target:self action:@selector(createChat:)];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(refresh:)];
UIBarButtonItem *deleteChat = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteChat:)];
self.deleteItem = deleteChat;
self.deleteItem.enabled = NO;
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
fixedItem.width = 155;
}else {
fixedItem.width = 75;
}
NSArray *toolBarItems = [[NSArray alloc] initWithObjects:newChat, fixedItem, refresh, fixedItem, self.deleteItem, nil];
[self setToolbarItems:toolBarItems];
[toolBarItems release];
[newChat release];
[deleteChat release];
[fixedItem release];
[refresh release];
}