2012-07-23 55 views
0

我有以下问题。我想从一个视图转到另一个视图,而不是使用按钮,而是编写代码。我有一个观点,其中有一些按钮。当我按其中一个运行if - else时。从别的我想去另一种观点。如何以编程方式在视图之间切换?

我已经试过这

GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:@"GameOverViewController" bundle:nil]; 

[self.view removeFromSuperview]; 
gOver.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:gOver animated:YES]; 

GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:@"GameOverViewController" bundle:nil]; 

[self.view removeFromSuperview]; 
[self.view insertSubview:gOver.view atIndex:0]; 

GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:@"GameOverViewController" bundle:nil]; 

[self.view removeFromSuperview]; 
[self.view addSubview:gOver.view]; 

,但毫无效果。有任何想法吗 ?

我也试过这种对

[self presentModalViewController:gOver animated:YES]; 

[self.view insertSubview:gOver.view atIndex:0]; 

[self.view addSubview:gOver.view]; 

感谢

GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:@"GameOverViewController" bundle:nil]; 

[self.view setHidden:YES]; 
[self.navigationController pushViewController:gOver animated:NO]; 

抛出exeption提前

+0

presentModelViewController应该工作酥料饼。在self.view已从其超级视图中移除后,您是否可以使用[self presentModalViewController],这只是一个好主意。你为什么这样做?当你想摆脱作为一个视图控制器的自我,那么你应该相应地解雇它。在适当的地方,您在那里关闭当前的视图控制器,那么您应该展示新的视图控制器 - 只有在您确实需要避免堆叠两个视图控制器时才需要。 – 2012-07-23 14:08:04

+0

难道你不应该将视图添加到已经从视图中移除的视图吗(这有点奇怪,但无论如何),只需用gOver.view替换视图? – boubou 2012-07-23 14:12:19

回答

1

鸵鸟政策呼叫[self.view removeFromSuperview];这可能会导致您的viewController使用presentModalViewController它时被释放,涵盖您的观点:

GameOverViewController *gOver = [[GameOverViewController alloc]initWithNibName:@"GameOverViewController" bundle:nil]; 

// gOver.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
[self presentModalViewController:gOver animated:YES]; 

如果你需要隐藏当前视图,然后使用:[self.view setHidden: YES];

+0

在 上抛出异常[self presentModalViewController:gOver animated:YES]; – panosbt 2012-07-23 14:27:52

+0

然后注释掉这行:// gOver.modalTransitionStyle = UIModalTransitionStyleFlipHorizo​​ntal; – 2012-07-23 16:33:02

+0

没有任何变化。再次抛出异常。 – panosbt 2012-07-23 17:03:26

0

“我想从一个视图到另一个视图,而不是一个按钮,但写代码。” - 你是把这个推到故事板上吗,还是你的意思是说你不想在按钮操作中想要它?

上面的代码工作正常:

-(IBAction)buttonAction:(id)sender 
{ 
GameViewController *game = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil]; 
game.modalTransitionStyle = UIModalTransitionStylePartialCurl ; 
[self presentModalViewController:game animated:YES]; 
} 

希望与您做ñiPhone应用程序,而不是一个iPad之一,在这种情况下,你需要使用的,而不是modalView

+0

我不想按钮操作。我只需要一些代码在我的if - else中执行按钮的工作。 – panosbt 2012-07-23 15:01:28

+0

你可以只发布你用来实现的代码吗?只是为了让我可以看到你是如何完成这项工作的 – darkmystel 2012-07-23 15:04:07

相关问题