iphone UINavigation Controller - iphone

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.

Related

Swift - Reuse the old instance of view when switch between Views and Passing data over views

I am trying to create a simple app and here is how it works:
When the user clicks the button "Picking", it'll show a tableview. And when the user selects a row in the table view, it will show the first view and display the data on the label.
I have 2 problems:
Every time I click the button "Picking", a new instance of the table view controller gets created; but does not reuse the existing one. How can I make it reuse the old one?
I want to pass the selected data to the root view. I tried by creating a segue to show the root view when user selects a cell and then passing the data on prepareForSegue method. But, I faced the above problem of memory leak. How do I pass the data between screens without leaking memory?
It sounds like you may be making a circular view hierarchy. When you tap on the cell in the table view, if you're pushing a new instance of the first VC, that is the wrong way to do it IMO.
Instead, you should pass the data back to the first VC then dismiss the second VC. Then, when you tap on the "Picking" button again, it's a new instance of the table view but your previous instance has already been removed from memory.
This allows for the smallest amount of memory to be used at any given time. When the table view controller isn't on the screen, it doesn't need to be in memory.
IMO, the best way to pass data up a navigation stack is to use a protocol and delegate. It's similar to how Objective-C protocols and delegates worked if you're familiar with that. Here is Apple's documentation on both. The delegate section is about 1/3 the way down the page.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

view controllers using storyboard

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.

Prevent Object Release in Storyboard

I'm looking after a solution to prevent object release in storyboard views.
Here is the deal, I have a storyboard view which contains data grabbed from JSON. This view has relation with another view (First View:List of items -- Second View:Item Details). Now when I tap on an item in first view, it goes to another view and shows the detail (using segue). BUT when I go back to the first view, it needs to grab the data from JSON again. (makes the user angry)
I'm aware of using Tab template, but I can't due to the application user requirements.
I'll be so much appreciated if anybody could help me.
Gratitude.
You are incorrectly implementing the Model-View-Controller pattern. Views should never fetch or hold data. They simply display it. View Controllers also do not hold data. Views and view controllers can be thrown away any time they're not on screen. This is by design.
Create a model class that is responsible for talking to the server and holding the resulting data. The controller should hand the model to the view, and the view should just display what it finds in the model.
I think you need to do a modal Segue from your First View to the Second View. Then when you are finished with the Second View execute [self dismissModalViewControllerAnimated:YES]; in the Second View to dismiss the Second View and return to the First View. This should then display the First View once more with the data.

iPhone: How to make UINavigationcontrollers inside eachother?

I have a navigation controller which I push a new tableviewcontroller for each received question to be asked to the user, so in stack it builds up to 30 controllers if there has been 30 question is asked.
The difficulty is that in some of the tableviewcontrollers I want to get the input from user by using another navigation controller inside that tableview: for example in the tableview(which is one the many views in the stack) there may be uitablecell with an arrow on the right saying choose the books you want to order, and when he clicks arrow a new tableview will slide in which includes the books to be choosen multiply with a checkmark, and then he will choose and back to main tableview and continue the main navigation there.
How can implement a new navcontroller inside the main navigation controller? or any better ideas?
EDIT: That can be an idea to push the detail view to the same controller, but then they will get popped immdidatly when user "backs" but I want to show the books (which are chosen in detail view) the main table, and when user clicks again to that cell, detailview will come with previous selected books. is this a good approach
Putting an UINavigationController inside a UINavigationController is a bad idea. What you seem to really want is persistence of state data after popping some of the view controllers off your navigation controller's stack.
I would put that state information into a persistent object. If it's a tiny amount of state data I may add that data to an existing object that I know is persistent, such as the app delegate or the root view controller. I would save a more complicated set of persistent data to a custom object. This object would be retained by the root view controller, and a reference would be given to each successive view controller. This object would have the data that may have been collected by a controller that was popped off the navigation stack and therefore no longer exists.
I don't really get why you dont just push the detailview of the uitableviewcontrollers to the main UINavigationController..
Could you explain that a little bit more detailed?
I have no rights to comment your question, so I am afraid I have to post this as an answer.
MfG,
SideSwipe

Pushing a new viewcontroller via a next button?

Sorry I can't show code for this question - it should be fairly simple to explain..
I have an array of data (object1, object2, object3, etc)… I have one tableview that lists these objects (object1.title, object2.title, etc) and upon clicking it push's a viewcontroller that shows the detail of each object. Now, rather than have to press Back on each detail view, I'd like to put a next button on the detail page..
I could easily push a new controller, however it would end with a chain of:
List View -> Detailview1 -> Detailview2, etc..
which would be a pain to traverse back up the stack.. Can the parent view controller be removed / changed easily or is this not the best way to do it?
Cheers.
You can always pop back to a specific index in the stack. So you could have a back button that pops back to the parent. If there is no real reason that the user would need to go back through the detail view controllers you could swap controllers by replacing the stack with a new stack using setViewControllers:animated: Look through the UINavigationController documentation and see whats possible.
This code will "pop" the view contoller off the view, so it removes it, instead of overlaying another view on top of it. Add this to your button action
[self.navigationController popViewControllerAnimated:YES];