Get selected value from UIPickerView custom delegate & data source - ios

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.

Related

Creating a Custom Picker View

I am trying to create a reusable UIPickerView not unlike UIDatePicker for use in multiple table views. The view works great, but as it's delegate needs to be itself (in order to set the components and rows), I cannot implement the didSelectRow:inComponent: method (to update labels and the model) in the tableViewControllers it is being used in.
How can I subclass UIPickerView and still provide a delegate? If UIDatePicker can do it, I'm guessing there's a pretty straightforward way.
Inspecting the UIDatePicker, it is a subclass of UIControl and follows the target-action pattern. It appears Apple just adds a UIPickerView as a subview to the UIControl. UIDatePicker has made it since the original SDK, so I think I will stick with this method for creating a custom, self-contained PickerView.
More information on custom implementing a custom UIControl: http://www.raywenderlich.com/76433/how-to-make-a-custom-control-swift

Read/Write a variable from another .swift file

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.

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.

Using to different delegate method for the same picker?

Im trying to create a view with two pickers, each with multiple components, one of the pickers has 4 components that are all text based, the other has two components that need to display graphics and two that need to display text. Is there any way to use the pickerView:titleForRow:forComponent:(NSInteger)component delegate method and the pickerView:viewForRow:forComponent:reusingView: delegate method for the same picker?
I've implemented both, only the viewForRow one seems to ever get called. Do I need to just use that one and create a view with a label for each row?
You can create one picker, yes, or you can implement one common method for both pickers and determine what picker is calling it by checking it's tag.
The Solution I cam up with was to call pickerView:titleForRow:forComponent: from pickerView:viewForRow:forComponent:reusingView: and create a UILabel with the string from the first method. It works, but it seems like there ought to be a better way.

Resources