Simulating the back button on the UINavigationController on the iPhone - iphone

I currently have a TableView inside a NavigationController where when a item is selected the following code is ran:
DetailViewController *DetailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:DetailViewController animated:YES];
[DetailViewController release];
DetailViewController = nil;
This loads up my detail view, however my question is this... How can I call the backbutton on the navigation controller without pressing it? Or how can set it back to the table view (which is what the backbutton does)?
Thanks for any help :)

You can call the popViewController method:
[self.navigationController popViewControllerAnimated:YES];

In Swift:
navigationController?.popViewController(animated: true)

Related

presenting a view not working in iphone

I am trying to navigate from one page to another on a button click in a facebook appliaction.
I am using
registerUser=[[RegisterPage alloc]initWithNibName:#"RegisterPage" bundle:nil];
[self presentModalViewController :registerUser animated:YES];
for presenting the next view after getting response from facebook.
But it is not showing the next view. It works fine in all other places where I used to present other views.
Anyone have idea about this issue?
What exactly is 'self' here? Is it a viewcontroller? Or just a UIView?
I think this'll only work if self is a viewcontroller or some subclass of it.
My code to present a view controller is somewhat like yours (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:controller animated:YES];
[controller release];
And to present a navigation controller it's like this (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:navController animated:YES];
[navController release];
[controller release];
Sometimes simply copying someones code helps.
thanks for everyones reply.i have solved it after a long fight.i just dismissed the view before pesenting the next view and set dismissmodalviewcontrollerAnimated to NO.[self dismissModalViewControllerAnimated:NO];
nextview = [[LoginPage alloc]initWithNibName:#"LoginPage" bundle:nil];
[self presentModalViewController: nextview animated:YES];hope this help someone like me

iPhone: change view when selevting a table cell which is in a tabbar

I have a UITabBar with 2 views.
One view contains a UITable. When I am selecting a cell I want to navigate to another view.
I always did it with this code:
if(self.damageController == nil)
{
DamageControllerOverview *viewTwo = [[DamageControllerOverview alloc] initWithNibName:#"DamageControllerOverview" bundle:[NSBundle mainBundle]];
self.damageController = viewTwo;
self.damageController.damageAccount = damageAccount;
self.damageController.ma = ma;
[self.navigationController setNavigationBarHidden:NO animated:NO];
temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[viewTwo release];
}
[self.navigationController pushViewController:self.damageController animated:YES];
But since I have the table in the Tabbar it is not working anymore.
Does anybody know why?
Best regards
Melanie
*Edit:
this is not my rootviewcontroller.
I already have a navigationcontroller and don't want to create a new one in the tabbar
What i understand from your question is that
1- you have a tabbar in the main window
2-then you have a navigationController in the tabbar.
so you need to push your controller to the navigationController this way.
[self.tabBarController.navigationController pushViewController:self.damageController animated:YES];
instead of
[self.navigationController pushViewController:self.damageController animated:YES];
I hope it helps

Using pushViewController: animated:

I'm trying to use the pushViewController: animated: with a UIViewController. I have it housed in a UINavigationController with initWithRoot and it still doesn't work.
Here is my code? Am I doing something wrong?
CloudappSettingsViewController *cloud = [[CloudappSettingsViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cloud];
[self pushViewController:nav animated:YES];
[cloud release];
It always crashes when it gets up to the [self pushViewController:nav animated:YES];
Any ideas?
Thanks
is "self" a UINavigationController?
It seems you are trying to push the navigation controller, but that is backwards. You should present the navigation controller and push additional views to it.
So the UIView you are in should already be in the nav controller then you would push cloud into that.
If this is in your app delegate, just add the UINavigationController as a subview to your app's window. If you want to present the UINavigationController as a modal view controller then do this:
[self presentModalViewController:nav animated:YES];
It will crash definitely because you set the nib to nil.
CloudappSettingsViewController *cloud = [[CloudappSettingsViewController alloc] initWithNibName:#"NibName goes here" bundle:nil];
Create a nib and assign it to your view controller.

navigation problem?

I have a view A where I have a code to push it to view B using:
[navController pushViewController:SecondViewController animated:YES];
when I get there and press back, the button says firstViewcontroller.
But when I press the buttons again to push it to the secondview again.
instead of showing 'firstviewcontroller' the buttons says secondviewcontroller.
and for some reason, my navigation-stack is adding and adding the secondview.
Can someone tell me what I do wrong?
I can't find any problem in my code.
Edit:
I have changed my code to:
FirstViewController *aFirstViewController = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:[NSBundle mainBundle]];
[navController pushViewController:aFirstViewController animated:YES];
[aFirstViewController release];
and it is still not working:S
I think u had not released the object of your SecondView.....
Anyways Put this at the place where u r giving your navigation code
SecondViewController *viewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
[SecondViewController release];

Viewcontroller not getting pushed to stack

I am fairly new to iPhone. I have a peculiar problem. I am adding a view controller as a subView to the current view. And then I want to push a new view controller from it. The problem is when I try to do pushViewController, it is not responding.
For example, in CurrentViewController I have added NewViewController's view as subView
[self.view addSubView : NewViewController.view]
Now From NewViewController, on the click of a button I am doing the following :
SecondViewController *secondVC = [SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
Here the secondVC doesn't get pushed to the stack.
If you have used view based application, You have to use this code.
SecondViewController *secondVC = [SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
// [self.navigationController pushViewController:secondVC animated:YES];
[self presentModalViewController:secondVC animated:YES];
If you want to use navigation controller in your appln. First you have to add navigation controller in your appln and then only will navigate the view.
Perhaps the problem is that method is called pushViewController not pushToViewController. Try
SecondViewController *secondVC = [SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
// don't forget release here
[secondVC release];