how to pass variables between a complex navigational stack - iphone

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.

Related

Swift Communicating between view controllers

I'm new to Swift and was wondering how I could set up segues between three view controllers. What I'm thinking is that the first viewController allows the user to choose between 2 objects, than redirects to secondView which allows to choose between 22 objects and based on the combination of the user's past 2 view controller choices, a new tableView is created. How can I trace the user's decisions and provide a tableView related to that in such a program? Thank you very much in advance.
I have Two rough solution for you.
(Not recommended) use two global variables VcOne and VcTwo. while selecting first object put a value in VcOne and when selecting from second 22 objects put value in VcTwo. and in tableView's Controller check both the variables and create your table View.
While performing Segue, have a Variable in destination ViewController and put a identifier. and have two variables on the Third ViewController. when you perform segue from second ViewController, assign Identifier of previous ViewController and current View Controller to next controller's Variables. and in tableView's Controller check both the variables and create your table View.
You can also use a array to keep track of user's selection.
Those are very basics of iOS development. just split the tasks that you need to do.
First split steps you have to do. First study about the navigation between View Controllers. in your case using segue. There are lots of questions regarding that in stack overflow.
then find out how to pass data between viewControllers. once you know about navigation it is very easy.
You can create logic with the values you are passing recieving.
ex: firstViewController will pass parameter called objectCount to secondViewcontroller. and based on that value count you can decide what are objects should show hidden.
And then based on that selection you need to get Object back to the firstViewController or go to another 3rd View Controller.
I'm really sorry but I don't think you can start developing iPhone application without understanding the basics.
You will need three ViewControllers and you need to use parepare(for:sender:) method to pass the data from one ViewController to another ViewController.
Here is an example on how to pass data between ViewControllers.

Add "next" button to display next table entity

Still completely new to this whole xcode thing so bear with me..
Im using xcode 4's 'master-detail' template with core data. When you click a cell in the table of the rootView, it segues to a detail view passing selectedItem as managedobject "detailItem".
I want to add a "next" Button (or a swipegesture preferably, if possible) that will display the next cell/detailItem, from the table in rootView, in the detailView
(exactly like the notes.app)
Do i need to recreate a fetchedresults controller and pass the objectIndex then fetch index+1 And display that as next object?
Or is there an easier more efficient way that wont drive me crazy trying to fabricate???
any help would be Most appreciated. And hopefully detailed as i would barely understand it
I would create a delegate relationship between the master and detail view controllers. If you don't know how delegates work, check documentation in that link, and plenty of good tutorials are a Google search away… this one's not bad. Here's the high-level approach:
On the detail controller, you'll create an (id) delegate property, and a "DetailViewControllerDelegate" protocol with methods like -(BOOL)detailViewShouldShowNextItem:(DetailViewController *)detailViewController and a similar one for previous.
On the master controller, declare support for the protocol in the header, and then implement the delegate methods so that they select the previous or next item in the list, loading it in the detail view.
Finally, after the detail view controller is instantiated, set its delegate to be the master view controller.

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

how to share data between views with navigation controller 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.

Best Way To Create an "Add" View Controller

I have a table view that contains a list of Project objects. When an item is selected it brings up a detail view. Pretty standard. What is the best way to implement "add" functionality (popup a modal view controller to input new values and save the item)?
Currently I have view controllers for my root view, detail view, and add view. Essentially the detail view and add view are exactly the same except for a save & cancel button in the add view. Is it possible to reuse the detail view in the add view?
Finally, what is the best way to display the list of project properties in a grouped table view separated into sections?
Thank you for your responses.
Most likely, you are already passing your detail view controller a managed object that it is supposed to display when in detail view mode. When the user decides to add a new project, just create a blank object, pass it to the detail controller and display it. (You might want to insert this blank object into another "empty" managed object context in case the user cancels the add process to avoid having to clean up your main managed object context in that case.)
The detail view controller would also need a flag that tells it whether it is in edit or add mode so it can adjust its controls (and possibly delegate messages it sends to its owner) accordingly. You would set the flag to the appropriate value before you display the controller.
It sounds like you're looking for a UINavigationController. The UINavigationController lets you push new view controllers on top of existing ones. It gives you a navigation bar at the top that will allow the user to go back to the root controller. I think it's the kind of controller Apple uses it in the default email application, to give you an example.
Concerning organization: you design your root view controller and a detail/add view controller. In your app delegate, you attach a UINavigationController to the window and you set its root controller to the main controller you want to display. That root controller can then push the add/detail controller onto the stack (and when it does so, it can tell the add/detail controller which types of button to display.)
I can't answer your grouped properties question, but it sounds like a separate question anyway.