How do I switch a new view controller in iOS development? - objective-c

I am trying to write an IBAction to switch view controllers for my iPhone application:
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:[NSBundle mainBundle]];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}
However, I am getting a build error noting that no interface declares "presentedViewController:animated:". Why?
Changing this to "presentViewController:animated:" produces the same error.

The method is presentViewController:animated:completion:, not presentedViewController:animated:

Instead of
[self presentedViewController: view2 animated:YES];
try
[self presentViewController: view2 animated:YES];

[self presentViewController:view2 animated:YES];

Try this your edited code.I think this will be helpful for you.
-(IBAction)changeToView2:(id)sender
{
if (self.view2 == nil)
{
view2 = [[UIViewController alloc] initWithNibName:#"View2Controller" bundle:nil];
}
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentedViewController: view2 animated:YES];
}

Related

How do I push many Views from one View using UINavigationController

I code many transition App using UINavigationController.
This is my transition image
"Main"-->"Second"-->"Third"-->"Fourth"
"Second"-->"A"-->"B"
"Second"-->"C"
"Third"-->"D"
"Main" is RootViewController.
At No2 and No3, if (["boo" isEqual:"foo"]){transit "A"} else if ("boo"...){transit "C"}.
I could make "Main" to "Forth", No.1 transition.
like this
ThirdViewController *thirdView = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdView animated:YES];
But I can't make "Second" to "A"and "B".
I thought to set UIRootViewController to "Second".
But "Second" needs to transit "A" and "C".
at "Second" like this...
SecondViewController.m
AViewController *aView = [[AViewController alloc] init];
[self.navigationController pushViewController:aView animated:YES];
How can I code?
Thank you! And sorry in poor English.
Please help me!
NSInteger index = 0;
for (UIViewController *view in self.navigationController.viewControllers) {
if([view.nibName isEqualToString:#"RootViewController"])//put any `XIB name` where u want to navigate
break;
index = index + 1;
}
[[self navigationController] popToViewController:[[self.navigationController viewControllers] objectAtIndex:index] animated:YES];
Try this code to push to a controller
DrawImgViewcontroller *ObjImageViewController = [[DrawImgViewcontroller alloc] initWithNibName:#"DrawImgViewcontroller" bundle:nil];
[self.navigationController pushViewController:ObjImageViewController animated:YES];
[ObjImageViewController release];
and use below code to go back
[self.navigationController popViewControllerAnimated:YES];
and use this to any controller you like
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:NO];//Instead of one you can use the currect index you need

How to dismiss and present the same modal view controller right after?

I want to make it seem as if it was a reset button.
It would take me to my mainviewcontroller screen and then come right back.
MainViewController.m
MainViewController.h
MainViewController.xib
ViewController.xib
ViewController.m
ViewController.h
I do not understand how I am supposed to do this.....
- (IBAction)newGame:(id)sender {
[self dismissModalViewControllerAnimated:YES];
ViewController *screen = [[ViewController alloc] inittWithNibName:#"ViewController" bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
Any ideas of how to do this?
Thanks
Do that without animation, as following.
- (IBAction)newGame:(id)sender {
[self dismissModalViewControllerAnimated:NO];
ViewController *screen = [[ViewController alloc] inittWithNibName:#"ViewController" bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:NO];
}

Is it Possible to call PopViewControllerAnimated and PushViewController for single click event

On a buttonclick action I want to destroy the current screen at the same time I want to navigate it to home screen instead of previous screen.
I tried to do that by Putting this code but didn't work.
-(IBAction) sel_goback
{
[self.navigationController popViewControllerAnimated:YES];
Home1ViewController *HM = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[self.navigationController pushViewController:HM animated:YES];
[HM release];
}
If self is the top view controller, then by the time popViewControllerAnimated: has returned, self.navigationController has been set to nil. Try this:
-(IBAction) sel_goback
{
UINavigationController *nav = self.navigationController;
[nav popViewControllerAnimated:YES];
Home1ViewController *HM = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[nav pushViewController:HM animated:YES];
[HM release];
}
However, "home screen" sounds like something that would already be on your navigation controller's stack. Maybe what you really want is something like this:
-(IBAction) sel_goback
{
UINavigationController *nav = [self.navigationController];
while (nav.viewControllers.count > 1 && ![nav.topViewController isKindOfClass:[Home1ViewController class]])
[nav popViewControllerAnimated:YES];
if (![nav.topViewController isKindOfClass:[Home1ViewController class]]) {
Home1ViewController *home = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[nav setViewControllers:[NSArray arrayWithObject:home] animated:YES];
}
}

How to use a UIButton to Change Views

How can I changed the view in iPhone development using an UIButton. I do not want to use a toolbar. Thank you
You must have a root view controller
This root view controller switches between the two views.
Something like this:
- (IBAction)switchViews:(id)sender {
if (self.vc1 == nil) {
self.vc1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.vc2 removeFromSuperview];
[self.view addSubView:vc1];
vc2 = nil;
}
else {
self.vc2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.vc1 removeFromSuperView];
[self.view addSubView:vc2];
vc1 = nil;
}
}

Check if controller has already been loaded XCode

I have the following code to push a new ViewController in a Split View Controller:
Level4ViewController *controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
The only problem I have, if I were to run this again, a new controller will show, I would like to be able to go to the view that I had before with all my data.
Can anyone help me out here.
Thanks.
EDIT:
Updated code?
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
else {
controller = [self.navigationController.viewControllers objectAtIndex:1];
}
}
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
UINavigationController has a property viewControllers which is a NSArray that hold all the stack that has been pushed to the navigation controller, in this array you can check for your view controller if it is there use that one - you check like this -
Level4ViewController *lvc;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
lvc = view;
}
}
and if you already knows that at which index your viewcontroller is there then you can get it from that index as -
Level4ViewController *lvc = [self.navigationController.viewControllers objectAtIndex:1];
update -
Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
if([view isKindOfClass:[Level4ViewController class]])
{
controller = view;
}
}
if(controller == nil)
{
controller = [[Level4ViewController alloc] initWithNibName:#"ModuleViewController" bundle:nil];
}
[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
If you are using a navigation controller
FirstScreenViewController *firstScreenVC = [self.storyboard instantiateViewControllerWithIdentifier:#"1S"];
if (![self.navigationController.topViewController isKindOfClass:FirstScreenViewController.class])
[self.navigationController pushViewController:firstScreenVC animated:YES];