view controllers using storyboard - ios5

i am trying to switch between two view controllers using storyboard. I create a modal seague by control-dragging (on buttons) from 1st view controller to 2nd, and then from the 2nd to the first.
So whenever I click on a button in 1st VC, it takes me to the 2nd VC. This time when i click the button on second VC, does it take me back to the original instance of the 1st VC or it creates a new instance?
If it takes me to the same instance, and user had written some data in some textfields, is there anyway to retain that on screen? (I might want to save them in some variables, and since the program will return back to the same instancem i'll be able to get the variables back)
If it doesn't take me to the same instance, is there any method to do so?
I tried making an instance of 2nd VC and using self.navigarionController push...(instance) but this doesn't swtich the controller.
If i do this pushing using the storyboard, and i do pop in my 2nd VC, it doesn't get popped either.
(and i would also couldn't understand the difference between push,modal and custom seagues)

Create the modal segue from VC1's button to VC2, but not the reverse one. When the VC2's button is tapped, call dismissViewControllerAnimated:completion: to return to where you were.
If you used a push segue instead, you would call popViewControllerAnimated: to go back but that only works if you have your view controllers managed by a UINavigationController.
You can think of push as a way of stepping through a sequence of related scenes while modal is something that's a bit out of the normal flow of the application. (That's not a firm rule but it's a starting point for deciding which way to go.) For 'custom', you write the segue code, so you decide what happens.

Related

Move Between different Navigation Controllers in Xcode Swift

I want to move from one UINavigatioController to other UINavigatioController. Scenario is First I am at ContactVC and after pressing any cell I will be move to ChatVC. Now when I press the back button on chatVC I want to move back on InboxVC. How can I achieve this scenario.
A UINavigationController is a stack based navigation system. This means when you navigate forwards you simply add another UIViewController to the stack, and when you navigate backwards you pop a UIViewContrller off the stack.
To do what you are proposing you will need to change the underlying UIViewController's that are currently in the navigation stack. You can do this by using func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) on UINavigationController programatically, but you can't do it through a storyboard as thats not how a UINavigationController is intended to be used.
UINavigationController controller keeps a stack of the views (in simple words to understand) when a push a new view controller onto it. So when you press back it essentially pops the last view controller to display its parent controller. This is also normal user behaviour on the iOS platform.
The behaviour you are looking in counter-intuitive to the user since you are breaking their flow of navigation. But if you can a have a strong case for it. You can manually create a back button which can push to the inbox controller to get this sort of behaviour in code or in interface builder.
Another way to achieve this perhaps what you can see in other apps is:
you have a Signup view controller (which might be the form). When the signup is completed.
change the root view controller of the window to Inbox controller and
The same time, push the Chat controller so when your user pushes the back button, they will be taken back to the Inbox.

Segues where a view replaces another

I'm planning a game app where a series of views are presented, one after the other, sequentially, in a loop.
I'm having a hard time figuring out the segue I should use for this (using the storyboard). It seems to me, that using the 'show' segue will result in a stack of views. In my app, you never go back from where you came, so does that mean that I could end up with dozens of views, one on top of each other?
Is there a way so that once you go into a segue, one view replaces the previous view?
Unfortunately you are not going to be able to do this with Storyboard segues as the "Replace" segue is only for SplitViewController (which sounds like wouldn't be right for what you are trying to do). Instead handle transitions through code. Have a base navigation controller and keep setting the new view controllers as the navigation controller's root view controller

iOS: return from pushed ViewController via delegate methods?

I've been wrestling with this for almost 2 hours now and no luck.
I have a View Controller. Lets call it First. I press a button and it takes me to Second view controller using a modal style. In Second I add some data which I can save or discard via two navigation bar buttons: Save and Cancel. both do what they do and then they return me to the previous screen. I implemented a protocol and I use the delegate. So far everything works fine.
Today I decided to have a Third View Controller which can be accessed from the Second VC. The problem is I couldn't move to THIRD until I changed the transition from First to Second to PUSH (instead of modal). Now I can go from First to Second and from Second to Third. All good again.
THE PROBLEM : If I press Cancel or Save on the Second VC, it calls the methods, it uses the delegate to go to FIRST and execute some function but when it calls:
[self dismissViewControllerAnimated:YES completion:nil];
nothing happens and Im stuck on the Second view. Basically I was using MODAL with Delegate and protocol methods... I switched to PUSH, and I cant get rid of my Second view and return to First. It seems like the dismissViewController doesnt do anything.
Any help is greatly appreciated. I can fix all this by removing the cancel button and using the BACK which comes by default with PUSH but I just want to know what the problem is with it.. THANK YOU !!
On your 2nd viewContrller call the UINavigationController's method popViewControllerAnimated: or popToRootViewControllerAnimated:. This will pop the controller (the one calling, which is the 2nd) off the nav stack. You can get the navigation controller in a UIViewController via its property navigationController.

Can someone explain UINavigationController setViewController and popToViewController

I'm familiar with the idea of creating a new viewcontroller and pushing it onto the stack. So far I have just created an instance of my view controller and pushed it. Now I am running into a problem that my 3 different view controllers are related ways of looking at the data. My RootViewController has 3 icons to start, and when you press on an icon, you push the first view controller. I currently do not set the view controllers in an array since I'm not sure what that buys me or how that works.
Scenario 1: Click on icon 1 (push vc1), click on a table in a popover in vc1, it'll push to vc3. Then you click on something in vc3, and it'll go to vc1.
Scenario 2: Click on icon 3 (push vc3), click on something and go to vc1.
So the problem I'm having is in scenario 1, it would seem to make the most sense to pop back to vc1 so they don't have a stack of vc1/vc3/vc1 and are looking at the data twice like that. But in scenario 2, since I started at vc3 instd of vc1, I should push vc1. But in both scenarios, the user is clicking on the same thing to go to vc1 from vc3, so how do I tell which viewController they came from in order to push or pop to vc1?
So I guess that's where I thought setViewControllers or popToViewController might come in handy, but I'm not sure how those work and if there's some simple example snippet someone can provide to get me started (assuming this approach is ok). Thanks!
It sounds to me, from your description, that you should be using a navigation controller that you push and pop onto and off. Btw, the navigation bar doesn't have to be visible.
It also sounds like you should sometimes pop the current vc before pushing the next vc. You will need to check the count of the viewControllers array - if the current count is greater than one then pop before pushing, otherwise just push as you are at the root view controller.
Or, you could use popToRootViewControllerAnimated:NO each time before pushing.
UINavigationController is what allows you to manage your views.
setViewController allows you to set which controller handles your view.
popToViewController takes the current view controller off the stack to the specified view.
IMO... The last 2 promote spaghetti logic and should be used sparingly.

iphone UINavigation Controller

I'm trying to get a better understanding of the UINavigationController. I have 3 .xibs. From .xib1 I am pushing to .xib2. I have to pass data to .xib2 from .xib1.
Controller1 *selectcity = [[Controller1 alloc]initWithNibName:#"Controller1" bundle:nil];
selectcity.item1 = #"hi";
// Push the next view onto our stack
[self.navigationController pushViewController:selectcity animated:YES];
[selectcity release];
I need to pass some data to .xib2 every time it opens that view. Pushing a new view onto the stack every time the user selects a row in the table, and then pressing back, selecting a row, back, selecting a row, back is creating a memoryWarning very quickly and killing the app.
If I add the view as a property and check if it already exists,
if (xib2 == nil) {
}
the viewDidLoad method only gets called the first time the view is called so I can't pass my data to the form.
I can't use viewDidAppear etc. because I don't want to the data to load when coming back from .xib3.
What is the correct way to control memory in this situation? Should I be popping xib2 from the stack every time they press the back button? Is so, what method would I do this?
Thanks for any help!
I'm trying to get a better
understanding of the
UINavigationController. I have 3
.xibs. From .xib1 I am pushing to
.xib2. I have to pass data to .xib2
from .xib1.
First off, you don't pass data between .xibs, you pass data between view controllers.
I need to pass some data to .xib2
every time it opens that view. Pushing
a new view onto the stack every time
the user selects a row in the table,
and then pressing back, selecting a
row, back, selecting a row, back is
creating a memoryWarning very quickly
and killing the app.
Please post more of the code related to this problem. Assuming you're talking about UITableView rows, your app shouldn't have any problems pushing/popping views onto the navigation stack in response to taps on rows.
the viewDidLoad method only gets
called the first time the view is
called so I can't pass my data to the
form.
Again, you want to pass data between view controllers, not views. You can do this quite easily by creating properties on your view controllers that you then set before you push the view controller on the stack. You are already doing this, I think with your item1 property.
What is the correct way to control
memory in this situation? Should I be
popping xib2 from the stack every time
they press the back button? Is so,
what method would I do this?
If you're using a standard UINavigationController to control the navigation stack, you don't need to do anything on your own to manage memory when the user hits the back button; the UINavigationController class will take care of releasing view controllers itself.