iPhone在navigationViewController模式下旋转视图
小时前我发布了一个关于在iPhone中组织纵向和横向模式的问题,现在我想我知道如何使用willRotateToInterfaceOrientation:duration。iPhone在navigationViewController模式下旋转视图
第一个屏幕是“地图视图”,其中一个按钮导致“设置视图”。地图视图不支持旋转,但对于设置视图,我为纵向和横向制作了单独的视图,并在旋转时相应地进行交换。
,
,
正如你可以看到当设置按钮按下SettingView被视图栈照常上加入。所以基本上我使用三个视图控制器;设置,设置风景和SettingPortrait。
我仍然发现在iPhone中使用navigationViewController时旋转视图的问题。 分段控制不起作用。它崩溃没有错误信息。它以前没有旋转就能正常工作.-当我不使用多视图旋转时。
rotateViewController.m
这是根视图控制器。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(IBAction) buttonPressed{
Setting *settingViewController = [[Setting alloc] initWithNibName:@"Setting" bundle:[NSBundle mainBundle]];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController: settingViewController];
[self.navigationController presentModalViewController:navController1 animated:YES];
[settingViewController release];
[navController1 release];
}
Setting.m
此视图控制器不只是交换意见时,旋转并显示纵向和横向之间相应的视图。
在Setting.m中,我换成如下的视图;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight) {
NSLog(@"to Right");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:@"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
NSLog(@"to Left");
SettingLandscape *setting_landscape = [[SettingLandscape alloc] initWithNibName:@"SettingLandscape" bundle:[NSBundle mainBundle]];
self.view = setting_landscape.view;
[setting_landscape release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(@"to Portrait");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
在viewWillAppear中,设置还具有视图控制器;
self.title = @"Shell ";
self.navigationController.navigationBarHidden = NO;
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(Done)] autorelease];
和完成是
- (void) Done{
[self dismissModalViewControllerAnimated:YES];
}
SettingLandscape.m
此视图堆叠时的视图上旋转。这个视图控制器有它的导航栏。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"Setting Landscape";
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
在viewDidLoad中;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"landscape:viewDidLoad");
//self.title = @"SettingLandscape";//not working!!
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done1" style:UIBarButtonItemStylePlain target:self action:@selector(Done)] autorelease];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
stringflag4MapType = [[NSString alloc] initWithString:@"blah"];
stringflag4MapType = [defaults stringForKey:@"flag4MapType"];
if (![stringflag4MapType isEqualToString:@"Hybrid"] && ![stringflag4MapType isEqualToString:@"Standard"] && ![stringflag4MapType isEqualToString:@"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:@"Standard"]) {
segmentedControl4MapType.selectedSegmentIndex = 0;
}else if ([self.stringflag4MapType isEqualToString:@"Satellite"]) {
segmentedControl4MapType.selectedSegmentIndex = 1;
}else if ([self.stringflag4MapType isEqualToString:@"Hybrid"]) {
segmentedControl4MapType.selectedSegmentIndex = 2;
}
以下调用不会被调用。奇怪。无论如何,旋转工作无关紧要。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation==UIInterfaceOrientationPortrait) {
NSLog(@"to Portrait");// does not print out.
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
if (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"to PortraitUpsideDown");
SettingPortrait *settingportrait = [[SettingPortrait alloc] initWithNibName:@"SettingPortrait" bundle:[NSBundle mainBundle]];
self.view = settingportrait.view;
[settingportrait release];
}
}
现在OK,你可以从那些抓拍看到有两个导航栏各有其栏按钮,完成和项目。完成按钮来自Setting和SettingPortrait或SettingLandscape中的Item按钮。所有按钮的选择器都是相同的,并返回到地图视图。按钮完成工作正常,但按钮项目崩溃。旋转后我需要一个导航栏上的按钮,就像后退按钮一样。我想我有一次'self.view = settingportrait.view;'问题就开始了。
我之所以需要项目按钮的工作是分段的控制开始崩溃,一旦我添加代码来支持旋转。如果我找到理由如何制作项目按钮 - 即在旋转视图内 - 工作,我想我也可以使分段控制工作。
您可以在https://github.com/downloads/bicbac/rotation-test/rotate-1.zip
- 下载整个代码来回答问题不看代码我最好的尝试(没有时间今晚:()
当你当前设置视图 - 控制模态,你的顶视图控制器是设置
旋转时发生,您加载setting_landscape或setting_portrait视图 - 控制,但只保留setting_landscape内部视图|。portrai吨。因此,setting_landscape/portrait viewcontrollers是已发布。当设备旋转时,它可能是“设置”viewcontroller接收旋转消息,而不是“setting_landscape/portrait”viewcontroller,因为它们没有推到viewcontroller栈。
因此,当您点击物品或细分控制时,它会调用代理,该代理可能已设置为发布已被设置为setting_landscape | portrait。
控制台中发生崩溃的消息是什么?
我的建议是建立分段控制设置视图控制器,然后使用“willAnimateRotationToInterfaceOrientation:duration:”函数将分段控件重新定位到正确的位置和框架。只需将YES返回到所有方向,旋转应该得到支持,不是吗?
为横向/纵向使用两个单独的viewcontroller的原因是什么? (我有时这样做,但很少)
编辑*您需要使用“willAnimateRotationToInterfaceOrientation”回调来更改动画,而不是“willRotate ...”“
https://github.com/downloads/bicbac/rotation-test/rotate-1.zip 此示例代码是惊人的对我来说解决我只通过简单的委托方法旋转视图的问题
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 返回YES; }
谢谢,我之所以用两个独立的观点是这样的,当我旋转视图我的UI布局剂量不适合屏幕。如果不使用独立的视图旋转,你如何管理用户界面呢?例如屏幕截图简化ü是。没有错误消息。 “所以,当你点击项目或分段控制,它会调用委托,这可能是设置为setting_landscape |肖像其中已经释放。” bicbac 2011-03-11 04:32:29
如果你想保持独立的视图旋转,然后构造两个视图中设置视图控制器和交换他们根据旋转。这样,视图的代表将设置视图控制器。 顺便说一句,你是什么意思的观点不适合?你想要一个完全不同的菜单模式?或者,如果你只需要左右移动的东西,使之贴合,只需设置帧/时中心“ - (空)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation时间:(NSTimeInterval)持续时间 ”功能。这也增加了所有动画元素的奖励。 – 2011-03-11 04:49:53
其实我打算让应用为iPad据我所知,每个视图必须旋转以获得接受。是的,我只需要移动它以适应它。我想我应该看看这个电话。非常感谢。 – bicbac 2011-03-11 06:57:31