Data transfer between two view controller - uitableview

i m pretty stuck on this one rite now let me explain the problem this way:
Motor View Controller (table 1)
Add New Motor (table 2)
show motor Detail (View 1)
edit Motor Detail (table 3)
now what happens is i go to table 1 then click on add button then go to table 2 and add a new motor on table 1 when i click on row of table 1 i go to view 1 where my details are shown in the labels and wen i click the edit button on this view a edit screen pops up now when i edit here i wanna save these details back onto my view 1 how do i do it ?( im using storyboard and all views are connected through segue's ) (basically i want to do edit function so wen the the edit screen comes up i want to see the existing details in my text boxes n den i get to edit dem n den save them back)thank you

I have two answers for you.
You need a permanent store for your data. Check out Apple's Core Data. They have documentation and an example. Also see RayWenderLick's Tutorial on Core Data. The answer is you will use Core Data to create a persistent store creating an sql database. The database will have an entity for motors. Each entry for the database will contain the detail for one motor. Each view controller (table) will access the database.
This solution provides you with a persistent store. If your power the phone off and on again you will still have your data.
A quick solution is to setup a delegate protocol which would allow the edit view controller to write data back to one of the other view controllers. Read Understanding Fundamental Design Patterns.
This solution may get you past your immediate problems but does not provide a persistent store.

Related

iOS - Views (ViewController), having persistent data

I am currently working on an app using Xcode/swift 4 where I need a view (lets call it loggerView), which is not the main view, to be able to take data from the main view (or other views) in a manner that allows said data to be shown whenever loggerView is displayed (using a text view in this case). I understand that I can use a segue to present/push this data (text log msg) to the loggerView upon transition to the loggerView, but there are a couple of probs with that:
When using segues the data can only be pushed all at once through segue prepare when transitioning and not piecemeal, which I need.
When the user exits loggerView back to the primary (or other) view all the data in the loggerView is lost.
Because of (2) I would need the primary view to store all logging data related to the information I want displayed in the loggerView such that upon segueing to loggerView all the data can be provided each time. This is not ideal because I don't want the primary view to store this, I'm looking for a way to fire this data to loggerView in the same way a delegate might work back from loggerView->primaryView where it can be called at any time whilst loggerView is being shown.
I think the main prob here is that I am working with a parent->child view situ (using navigation controller) where every time we transition back from child to parent the child view (loggerView in this case) is removed from memory and so all data pertaining to loggerView is deleted, thus the need to segue everything each time loggerView is opened.
Is there a way to make more than one view permanent (memory wise) and then send data between primaryView and this other view on an ad-hoc basis rather like the way delegates might work in reverse (loggerView->primaryView)?
Summarizing our discussion in the comments:
Your objection to storing the child's data back in the parent is probably misplaced. Such behavior is normal and standard. If you insist on being different, you could keep the data in UserDefaults, as being both global and persistent, but that is usually regarded as a crutch.
In effect, your desire to persist the view controller merely in order to persist some data that it has fetched from the user is just wrong. Suppose a dialog pops up asking you to enter a name. You enter the name and tap OK, and the dialog vanishes. You do not believe that the dialog should persist behind the scenes just to hold the name, do you? No — what persists is the name. The dialog, and all the mechanism that enabled it to appear, was merely temporary in order to manage the user interface.
It might help to repeat "model view controller" (MVC) to yourself over and over as you work. The data collected from the user is model. If you want it to persist, persist it. But do not persist a view controller merely in order to do that. Think of view controllers as nimble; their purpose is to come and go, as the views that they control come and go. If you have data that needs to persist, it needs to be passed around, or put in a more persistent place. Model, view, controller. Three different things. Do not try to turn the controller into the model!

How to relate data to a specific row in a UITableView

What I basically want is to show the user's name in the first tableView but have a disclosure indicator (segue) to go to a second tableView which will show the history of the selected user. I know how to load data into a table and how to segue from one row to the detail table but I just don't know how to show different data for each user. I know this may be out of my scope of knowledge at the moment but I would like to give it a try.
1- What would be the best way to store the data for each user (history), an array per user?
2- How can I relate data to a specific user so it loads when the user's name is touched?
3- How can I capture the row being selected?
Any suggestions will be appreciated.
Honestly, you shouldn't worry about it being outside of your scope of knowledge, as that's how people learn. You will need to do some amount of work, however, and that part can be a little difficult.
Luckily, what you're trying to do is a very well documented function and Xcode even provides a template for master -> detail views.
Check out this wonderful tutorial by Ray Wenderlich on creating a simple master-detail application: http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1
His other tutorials will also be immensely helpful to beginning iOS development: http://www.raywenderlich.com/tutorials

Maintaining information between viewcontrollers

I am pretty new to stackoverflow so please let me know if there's anything I should add or leave out in my posts from now on.
problem:
I have one original view controller that has a tableview on it with 2 options, "Date" and "hairstyle" the user is able to click on one of these table cells and it brings them to a new view controller where they can select a hairstyle/date. When they click done, they're chosen date/style reflects on the original view controller. The problem is, that it won't reflect both choices at once. Whenever you try to reflect one, the information from the other is reset for some reason.
Feel free to let me know if there is any more information you would like me to share in this post. Thanks for all the help.
Whenever your detail view controllers are about to pop back to the original view controller you can send the data using prepareForSegue.
In order to give you more detailed info we need more details about how you are doing things.
Eagle11 is right you need to use prepareForSegue, before jumping to code you better check this very useful(for me) links:
Passing Data between View Controllers
iphonedevsdk (my personal favourite)
Storyboard tutorial
Passing Data Between VC
Some issues:
passing data between views

How could I drag a tableview from up to down and fire an action then?

I have in mind following behaviour for my table view controller. It is a table view to show to do items, just like a tasks list. I want to know if it where possible to drag down the whole table view and hold it in this position, and while in this position the user could enter a voice command that should be stored as a sound file in a core data entity.
Any proposals and help is welcome.
There is a project that implement PullRefresh, but you can use this to do another stuff.
Take a look to EGOTableViewPullRefresh

Saving Array content over two view controllers

i have a slightly confusing problem here. I am using storyboard i have a tab view controller tab 1 has a table view controller (table 1) which on selection of a row goes to table 2 and some data is added to the table 2 here n it is shown on table 2 cell when i navigate back to table 1 and go forward again to table 2 that data is lost ..how can i save the data then so that it can still show the saved contents. thanks
It's because the navigation controller releases the second view controller when you go back from table 2 to table 1. If there were no other references to the 2nd table view, it gets deallocated. I suppose you're also creating the table view every time you select a row in table 1. Try to keep a reference on the second table view, and not re-create it every time, only on initialization.
Sounds like you need fixed data storage. You can use Apple's Core data for this. See Apples documentation on Core Data or see "Pro Core Data for iOS" by Michael Privat. Then each view controller can store and retrieve data from the permanent store. Be aware that learning core data takes some effort, but it is worth because you will find yourself using it time and again. I also ran across this project arraydatatable which provides a structured data solution without using core data. Also, since you mentioned that your are new to iOS you should read Apple's documentation on their recommended Model View Controller (MVC) structure. In that structure the Model is your data, the View is what you see on the screen, and the Controller controls the view as well as fetching and storing data to the model.

Resources