Iphone Save data and go back to the previous view controller - iphone

I have a Table View Controller which I have designed using storyboards. This has an Add button on the navigation bar and I created all this using storyboards. On Add button clicked I have a view controller view to add data and this has a save button on the navigation bar. On Save button Clicked I want the user to save data and return to the TableViewController. All the design was done using storyboards. On Save button clicked I am calling the IBAction save method. I am able to save data but how do I go back to the previous TableViewController programmatically.
thanks
prerna

In your IBAction save method, call:
[self.navigationController popViewControllerAnimated:YES];

Related

UINavigationController popToRootViewController and P

I am working on app with UINavigation.
First view has two buttons, 'AddNew' and 'Show All'.
flow of app is :
When user selects first view, it will push ViewController with textView and a button.
This view will push another view controller with tableView to select one of the items,
after selection, this view push a viewcontroller with summary of selected Items and a button to save options.
When user tap on save button, it will do some calculations according to selected data, save values in database and pop to root view controller and open show all viewcontroller.
My Problem :
I tried using poptorootviewcontroller on save button and pushviewcontroller on firstView's viewdidappear (checking with flag). but it seems that navigation controller's animation is not completed when I tried to push viewcontroller.
I also tried poptorootviewcontroller without animation, but not working.
I also tried on viewdidappear of firstViewcontroller, performselector:afterDelay: but still no success.
What is the best way to achieve this?
Thanks,

making a custom back button in iphone

I am new to iphone so I have a very simple question. I am using a storyboard in xcode 4.2. I have multiple screens with about buttion in each viewController.
Now I have created segues ofc. Each about button i have linked with about viewController. However I am not using a navigation controller.
I have custom made a back button. How can i go back to the screen which brought me on the about section.
Best Regards
If you are modally presenting the new view controllers they can be dismissed using the following.
-(IBAction)myCloseAction:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
As you are not using navigation controller, u need to manually do the pushing and popping viewcontroller. When u change the viewcontroller, store previous viewcontroller reference in the new viewcontroller and when back button is pressed goto the previously stored viewcontroller.

Change view controller on press button

I have two View Controllers, I need to have a button in ViewControllerOne that when I press it Show me ViewControllerTwo.
In storyboard I related both views with a "Presenting Segues" - Push modal. And both views have a view controller class.
I'm not sure what you mean by "I related both views with a 'Presenting Segues' - Push modal." Are you using a navigation controller and want a push segue, or do you want to do a modal segue? A "push modal" is a contradiction in terms.
So, let's imagine that you want a modal segue. So, you put a button on the first view, right-click and drag (or control-click and drag) from that button to the second view.
You'll get a pop up asking for type of segue. Select "modal".
And you're done transitioning from 1 to 2. No code necessary.
If you want a button on the second view to take you back to the first view, you do not want a modal segue from the second view back to the first view, but rather you want to dismissViewControllerAnimated. You can do this via a custom segue, or easier, just have a button which calls dismissViewControllerAnimated. Thus, you'd add a button to the second view, and while the editor is in in "assistant" mode (where the associated .h file is showing below the interface builder; see below if you want to know how to show the .h file at the same time as the Interface Builder screen), right-click (or control-click) and drag from the button on the second view down to the second view controller's .h file:
By the way, if you don't see the .h file there, click on the "assistant" editor button and choose "automatic" for the files to be shown down there, and you should be good:
It will then show you a pop up asking you what you want to do. Select IBAction and give your new method a name:
Then go to your code for the view controller, and add the dismissViewControllerAnimated code:
All that code says (and in this example, I just called my IBAction dismissTwo) is:
- (IBAction)dismissTwo:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
If you want to do a push segue, it's even easier. First, if you don't already have a navigation controller, add one by selecting the first view and choose "Embed in" - "Navigation Controller":
When you do this, you'll have a new navigation controller (which you don't really need to do much with) and the first view will have a navigation bar.
Now, right-click (or control-click) on your first view's button and drag over to the second view:
This time, select the "push" segue:
And you'll know that it worked, because your second view will have a navigation controller
You don't need a button to go back, because the navigation controller will automatically have a "Back" button, so you don't need to add your own.
This is how you achieve a push segue.

performSegueWithIdentifier isn't working

I have this code running when I tap a button:
[self performSegueWithIdentifier:#"services" sender:self];
NSLog(#"ButtonClicked");
I can see the log message appearing each time I press the button but the segue isn't activating.
In my storyboard file I have a segue named services, that's the segue I want to activate.
Any idea where I'm going wrong?
Edit
The button is inside a tab bar controller, will that affect things?
For push you need to embed your source view controller in navigation controller.You can do this as follows:
Select your source view controller, Go to Editor and select Embed In
-> Navigation Controller.

UINavigationController doesn't fully push view and only changes the Navigation toolbar to the next view's toolbar

So I have an iPhone application that utilizes a UINavigationController for setting up the views. When the application first starts, it presents the user with a UITableViewController and the user can select an item and it will push another view. Now I have it set so that my app remembers the user's last selection and automatically selects it and loads the correct view controller. The only problem is that I am experiencing a really weird glitch when I load the next view automatically..
When the view is pushed, the navigation toolbar will change so that a back button directed to the previous view is showing but it won't display the next view. It will instead keep showing the table view and I can interact with it as well. I can press the back button and it will change the toolbar back and the tableview is still shown. Then when I select an item it loads the view just fine.
Thanks for the help.
Code:
I determining whether to push the view controller based on whether it can connect to a server. I do this in a backround thread:
- (void)startingThread
{
[NSThread detachNewThreadSelector:#selector(loginThread:) toTarget:self withObject:communicator];
}
- (void)loginThread:(MowerCommunicator *)communicator
{
//If it can connect, launch thread complete.
[self performSelectorOnMainThread:#selector(loginThreadComplete:) withObject:communicator waitUntilDone:NO];
}
- (void)loginThreadComplete:(MowerCommunicator *)communicator
{
//push view controller
}
Now I have added NSLog statements to track if the view is actually "showing" and both viewWillAppear and viewDidAppear get called. I also check the delegate methods for the navigation controller and they get called as well.
I have a view that is the initial startup view and it reads from a server to determine what to display in the next table view. That gets pushed fine and when the tableview gets pushed, I hide the back button so the user can't get back to the first view without closing the app. Then the tableview looks at a variable in NSUserDefaults to determine with there is a saved index and then pushes the next view controller. That is when the glitch occurs. If I then press the back button to "go back" to the table view (this really just changes the navigation toolbar) and then I select an item from the table view, it loads the next view correctly. Also, I call the exact same methods when the user pressed an item from the table view and when the app loads the view automatically.
The simplest explanation is that you're pushing the initial tableviewcontroller onto the navigation controller's stack twice.
If you have the initial tableviewcontroller set as the nav's first controller in a nib but then you push the same controller onto the stack when trying to automatically display the stacked views, you would get what you are describing. The nav starts out with the controller from the nib and then you push another instance on top of that.
You should log the nav's viewControllers property before and after you do the automatic push to see what the actual state of the stack.