how to share data between views with navigation controller iphone - iphone

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.

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

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.

iOS tab bar object holder

I am new to Obj-C so I will be happy for any best practice idea or something.
I have a table view with some objects. When any object is selected I would like to open an object detail view with tabs. These tabs divide the object properties to some logical groups.
But it still describe one object and I would like the user has an experience when creating the new object he can switch between tabs and change the values without any saving data until the save button (common for all tabs in navigation bar) was clicked.
Thanks for any help.
Not quite sure what you are asking for. I think you can use UINavigationController to lead the user to the detail screen, in the detail screen use UITableView to display your object detail. If you want to group them, you can set the tableView style to Grouped.
If you are asking for something else, please provide more details.
To be idiomatic with the iOS experience, displaying the Tab Bar should be a modal dialog with a "Done" button then.
Create a UINavigationController and place the UITTabBarController as the root controller of it, then in the navigation bar of the root controller place 'Done' button.
Note that you must keep a temporary model of whatever you are editing in memory (I would suggest subclassing UINavigationController, adding a property for the object and passing a pointer to the object to all of the tab controllers), since the views containing the changed data can be deallocated at any time when they are not displayed to save memory.

how to pass variables between a complex navigational stack

I have several uitableviews that the user can use to set different types of search parameters in my application, I will allow the user to select a cell that will then push a new view onto the stack in which the user can choose what to search and then return back to the original view with the value passed into the originally selected cell..
Where this gets complicated is that each of the original search views will be able to push the same view for search results onto the stack..
here is a graphical example of what I mean as it is hard to explain.
In this example only one viewcontroller can show at a time when a cell in the current view controller is selected the subview is loaded with the list of options, both view controllers use the same subviewcontroller.
My question is to do with returning values that you select in the subviewcontroller back to the correct viewcontroller...
I am trying to figure out how to return the selected value in the subviewcontroller to the correct cell in the correct viewcontroller.. I am hoping to get some suggestions on the correct way to handle this type of view structure as its not really covered in many books that I have available to me, plus I hope to draw on your guys knowledge and experience with similar projects.
One common way is to define a protocol in the sub view controller for callbacks, and have each of the main view controllers implement that... when they create or call up the sub view controller, they tell it that they are the selection delegate to be called when a change is made. Then they can also choose to dismiss the sub view controller.

How to use a tableView as a selector to send values to another viewcontroller?

I'll try to explain this as best as i can, but i appologize if it gets too confusing - I've been stuck on this problem for many hours now.
In my application i have a search screen where the user will be able to select a bunch of criterias to perform the search by. Some of these criterias consists of fairly long lists of values to choose from and therefor i want a tableview on my searchscreen which have 4 rows - Each row representing a criteria that the user can set.
Once the user clicks on a row i want to push a new window in my navigationcontroller which consists of a new table containing the selectable values for that criteria - Once the user clicks on one of these rows on the new window, i want the selected value to be sent back to my main searchscreen and pop back to my search screen.
What would be the best way to do this?
Elaboration:
My searchscreen is called SearchViewController and is contained in a navigationController.
SearchViewController contains two sliders and a tableView with 4 rows called "Searchtype", "Property type", "Salestype", "Area" and a searchbutton. If the user clicks on "Searchtype" then i want a new view to be pushed in the navigationController which should contain a new tableView with a bunch of rows representing different possible values for the "Searchtype" criteria - The same goes for all 4 rows in SearchViewController.
When the user clicks on one of the value rows in the newly pushed tableView i want that tableView to be popped away and the selected value sent back to the SearchViewController allowing the user to either select more criterias or push the search button to actually perform a search based on the selected criterias.
But i can't figure out the best way to do this?
I really appreciate any help i can get - I'm going nuts trying to figure this out :)
Btw. i don't use Interface Builder - All UI elements are coded manually.
5 ways to do it here:
1) Let the Search View Controller be the delegate of action in the Search Type View Controller, so that when the user selects a search type, it will be informed. Use protocol for proper check at compile time if you want, and remember to use assign instead of retain for the delegate, to avoid circular reference.
2) Set the UINavigationController delegate to Search View Controller (or whatever class you want to control it), and listen to the event when the Search Type View Controller is popped out.
3) Implement a "refresh" function in viewWillAppear: as suggested above, but this is not recommended, because the implementation of viewWillAppear: sucks and not reliable at all. Maybe good for simple app, but when the structure of your app gets complicated, forget it.
4) Use NSNotificationCenter. Your Search View Controller will observe all changes to search criteria, and in each child view controller, when the user changes it, post a notification. This is more complicated, but much more powerful and flexible than all the methods above.
5) Similarly, you can use Core Data to store all search criteria in an object, and listen to changes in that object using KVO. This is a bit more advanced and may be overkill, but if you know KVO, it makes life much easier in Objective C, so probably worth taking a look anyway.
Btw: It's good to do all the UI by hand coding to understand the concepts at first, but try to move to Interface Builder whenever you can. It is a much recommended way to work (there are countless threads on this in Stackoverflow or on the web, with more elaborated discussions on why IB is better).