panosbt
panosbt

Reputation: 1

How can I switch between views programmatically?

I have the following problem. I want to go from a view to another view, not with a button, but writing code. I have a view in which there are, among other things, some buttons. When I press one of them runs an if - else. From else I want to go to another view.

I have tried this

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

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

this

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

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

and this

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

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

but nothing worked. Any ideas ?

i also tried this

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

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

throws exeption on

[self presentModalViewController:gOver animated:YES];

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

[self.view addSubview:gOver.view];

Thanks in advance

Upvotes: 0

Views: 198

Answers (2)

Sverrisson
Sverrisson

Reputation: 18157

Don´t call [self.view removeFromSuperview]; which may cause your viewController to be deallocated, when using presentModalViewController it covers your view:

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

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

If you need to hide your current view then use: [self.view setHidden: YES];

Upvotes: 1

darkmystel
darkmystel

Reputation: 198

"I want to go from a view to another view, not with a button, but writing code." - did you refer this to push in storyboard or did you mean that you din't want it in a button action?

The above code works fine :

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

And hope that you doing n iPhone app and not an iPad one, in which case you need to use popOver instead of modalView

Upvotes: 0

Related Questions