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.
Related
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
I am writing an iOS / CocoaTouch app and I am facing the following problem :
I have a detail view (think of an overview of one given object)
This detail view can present other elements
Any of these other elements can be viewed in this exact same detail view (I mean, another instance of this view / view controller using the viewed object model.
The only problem I have is that I am not able to create a segue from a view to the same view in the storyboard editor. Therefore, I cannot create the segue at all, cannot assign an identifier, and thus cannot trigger it from code.
Is there any way to implement this ?
This is as simple as a detail view pushing another, each of them having one dedicated instance of the view controller with their respective object model.
Thank you so much, I looked everywhere and cannot find any topic related to this.
Christophe.
Segues are between view controllers, not views (even though a view can act as a trigger). If you want to have a segue to a new view controller, create a new instance of it in the storyboard, assign its identity to the same class as your original detail, and define the segue.
If you're only trying to change which view is displayed inside a single view controller, then selectively setting views hidden and not-hidden can work...or adding/removing sub-views.
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
i have a view which is fetching data from an Array and presenting the data in a tableview. This view has a navigation controller with a button in it. The button is meant to take you to another view for advanced searching. Let's say that in this new view i have a picker, when the user selects a value from the picker and clicks the back button in the navigation bar i want to get the value that the user selected. What is the best practice to do that? How can i send the selected value from one activity to the previous one?
Thanks in advance.
What you need here is reasoning a bit in terms of the Model-View-Controller design pattern.
Views should get their data through the model. So in your advanced search view, when the user selects some value, this value is stored in the model.
When you go back, the first view redraw itself by reading the current search value from the model.
There are other possibilities, like having the search view controller own a pointer to the first view and sending a message to it when the search value changes, but this is not very modular and is pretty fragile.
Use delegation. Write your own protocol like "PickerViewDelegate". then implement this protocol in your "main view" (which has table view). in PickerView just invoke [delegate somethingPicked:something].
I'm not sure, that search value is a model entity.
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.