Read/Write a variable from another .swift file - ios

I have two ViewControllers and both with their .swift class file.
The thing is i have a variable declared in the first ViewController, and it will be filled with a value entered by the user in the first view. But then i want to show that value in another view (im using a tab bar controller).
But the problem is i don't know how to reference to that variable from the SecondViewController.swift because the variable was declared in FirstViewController.swift

for the protocol/delegate method of sharing data see:
https://medium.com/swift-programming/ios-swift-protocols-and-delegates-7193f7f58b8a
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views
for saving to file using NSCoding, I posted code earlier today here:
SWIFT How to create a NSCoding Subclass and call it from another class?
If you want to save more simply with NSUserDefaults:
Store [String] in NSUserDefaults (Swift)

There are two common ways to move data. If you just want instance data (the current instance of ViewController1) to move to ViewController2 you would pass the data via a segue and the prepareForSegue function. This is one of the most important thongs to learn. I will link to some tutorials below. Keep in mind, Xcode 6 has evolved and the syntax may change slightly. So for the function prepareForSegue, you need to look it up in the documentation by command-click on the word "prepareForSegue" in your swift file and it will take you to the documentation where you can cut an paste.
http://makeapppie.com/2014/07/01/swift-swift-using-segues-and-delegates-in-navigation-controllers-part-1-the-template/
http://bharathnagarajrao.wordpress.com/2014/06/17/swiftly-learning-swift/
The other common way to pass data is to have the data persist by saving to a file. Another set of links. I would also recommend going to github and search for projects with both prepareForSegue and Swift for further examples.

Related

What's the best way to load data for a ViewController before navigating to it?

In my application there are two view controllers that navigate to a DetailsViewController.
Right now, when the DetailsViewController appears, I fetch data from the server and display it on the UI. I dislike this because the UI is blank while the network request is going on. What I want is that the data be loaded in the previous view controllers and then passed to DetailsViewController.
Now the problem is that I have the exact same "load-data-and-then-push" code in two view controllers and I'm not sure what the most sensible way is to remove the repetition.
One idea is to have the two view controllers inherit from a common superclass which contains the loading/pushing method. I don't like this strategy because, supposing I have more ViewControllers like DetailsViewController down the line, I wouldn't like to write a loading superclass for each one.
Another idea would be to define a static method in the DetailsViewController which the two view controllers can invoke but this method contains UI related code (specifically, code to show an HUD Progressbar and a UIAlertView in case network fetch fails) which makes me uncomfortable.
I am very new to iOS and Objective-C so I might be missing something simple and obvious.
My favorite would be in this case to create a new class which handles the loading of the data (like http-request, etc.) and to create a delegate protocol for this class. This delegate callback might then be implemented in your two viewControllers which would then perform the push segue to your DetailsViewController when called. Delegation is a very nice and powerful feature, check out the documentation here: Delegation
Well, I'd better write it in the comments, but I have no reputation for that.
Imagine you are reading a json with several students information (name, year, etc.)
you can create a student object with the property that will be read and an object that will have a method that will run in the background that will be responsible for accessing your WS (JSON or whatever it is) and record this information to the student object. So if you have 10 students you will have an NSArray containing 10 students. This array is what you will for your next viewcontroller.
It is a lot of code, but you think examples easily.
If you use Storyboards you can use prepareForSegue: sender: to pass your data/model class to your DetailsViewController. If you use xib's you can do the same after instantiating the DetailsViewController and before pushing it.
If you need to load subsequent data from your server you should write a class that does this network stuff for you.
If DetailsViewController needs to load some additional data, you can use something like a loading view like Andy suggested. This is a widely used method.

Get selected value from UIPickerView custom delegate & data source

So I cannot provide you with any code, but it's quite simple what I want to achieve.
I am searching for a way to get the selected value from an UIPickerView in iOS 8 (Swift)
these pickerviews have a custom delegate & data source.
So the method of didSelectRow is accessed in a custom class, not the ViewController, but I would have to be able to get the selected value and use it in the ViewController. Let's say, place it in a textbox.
I can't find a way to do this, should I try to access the delegate itself or what should I be doing?
Care to share your knowledge with the students of Application Development? :)
The UIPickerView class has a method named: `selectedRowInComponent'. This function may help you select components in a picker view. Apple Documentation for UIPickerView class would help you with your project.
Also make sure your view controller conforms to the UIPickerViewController Delegate and Data source.

Same UIPickerView in different ViewControllers

I'm new to objective-c and iOS app development, and I have next question:
Is it possible to have one picker view in different views, I mean it should have same data in it and behave equally. Currently i have added 3 picker views, each in different view, and now i have triple copy of same code. So to avoid code duplicating, is there way to create custom view and put this in each of 3 view controllers and just initialize it. Thanks.
Sure. You could even do it all from a separate NSObject inherited class.
Then make that picker class handle all of the picker stuff via its methods.
Then the view controller that wants to use it only has to have an instance of this picker object and it directly calls the needed methods.
That or you can directly create an UIPickerView inherited object and automatically setup its data when it's initialized and use that instead of UIPickerView.
Some quick example code can be found by googling. Here's one: http://iphonedevsdk.com/forum/iphone-sdk-development/46378-subclassing-uipickerview-question.html

How can i access a ViewController that's a segue from a Tableview in another ViewController from my AppDelegate?

I'm new to CoreData, and my problem is that i want to make an NSManagedObject from a ViewController Class that has a table view, basically i want to save an object inside a tableView "every time the user adds a cell", but that ViewController is presented from a tableview menu, "object at index", the question is, how can i save data for that
ViewController from my AppDelegate? or how can i access that ViewController From my AppDelegate...
Here's an image of my Storyboard and somehow explaining what I'm trying to achieve.
Thanks!!
I've read through your question ten times and i don't seem to be understanding it fully.
From what i understand you want to save the data into core data from the bottom tableviewcontroller which is a UIViewController with an UITableView as a subview.
I don't see how that should be difficult unless i understood the problem wrong.
The basic Xcode template for an iOS project using Core Data implements the initialization within the app delegate but relying on the app delegate as a connector is something i don't do. I always abstract that information into a singleton which can be accessed from any class that implements that logic.
Also the basic Xcode template for a core data project also contains logic for adding entries within the tableview so i would recommend looking at that.
If you want specific example source code i can do that but i recommend looking at the core data template.

Custom ViewController objects don't persist through View changes

I have no idea if I'm using the right terminology here, but I'm hoping someone can help - I'm pretty new to iOS Dev and have run into a problem:
I have created a custom class that is used to store an object. It has a bunch of properties and functions, just as an object should. This object is being declared in my ViewController's .h file, and initialised and used throughout the ViewController. The object holds a bunch of information about a test, and takes some measurements in various threads.
The problem I'm facing is that when I load another view (using ECSlidingViewController for the menu) and then return to the view which had the object...it seems to have forgotten the object. The tasks running in the threads are all still running, but that instance of the object seems to be gone.
Is there a way to preserve an instance of the object when changing views so that when I return to the appropriate view, the object is still there and I can still use it?
Thanks!
I'd recommend you to put the objects reference in a transversal class, something like a manager or any other class which you consider appropriate according to your design and most important it needs to be a class that you're completely sure won't be release nor re-created as view controllers are usually when you change from one view controller to the other.
A singleton instance that manages your main logic could be a good option.
Turns out that my menuViewController that was implementing the ECSlidingViewController was recreating the view that contained the object each time I selected the item from the menu. Thanks #rdelmar for pointing this out!
The object was persisting, but in a viewController that was being replaced each time it was selected from the menu and hence the object was unreachable.
I simply implemented this and references to the viewControllers are now being stored in a mutable dictionary and simply recalled when the view is needed, rather than recreating the view. This means that the viewController is being reused and the object is persisting with it.

Resources