Using to different delegate method for the same picker? - ios

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.

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

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 to make my own UIPickerView?

Since UIPickerView can't be customized to the level I want (without taking a risk by hiding specific subviews), I need to roll my own to select a time.
I figure I can do this using two UITableViews, but I'm unsure of how to do 2 things:
1) How do I determine which cell is in the middle of the view (i.e. which one did the user select)?
2) How do I make the table snap to the cell nearest to the middle of the view once the user has stopped scrolling?
Thanks for your help.
Why do you say that UIPickerView can't be customized?. It has a bunch of delegate methods made to allow a lot of customization, such as view for rows, height for rows etc etc..
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerViewDelegate_Protocol/Reference/UIPickerViewDelegate.html
Implement the delegate in your ViewController and customize it as you want.
You're just trying to select a time? Then use UIDatePicker. It is a picker view for time/date.
Depending on the type of customisation that you want, you might not need to make your own UIPickerView. You can return your own views for each component using UIPickerViewDelegate. Check pickerView:viewForRow:forComponent:reusingView:
The free Sensible TableView framework has a UITableView based date picker that you could use out of the box. Should save you a lot of time as it handles all the scrolling and table view resizing. Hope this helps.

Reloading Data in a Custom Picker

I created a custom Picker with a nice X-ray lens glow effect for the target selection. It is just made up of two UIScrollViews with the same content offset. When the label goes under the lens it appears to glow. Anyways, I need to dynamically update the data and if it were a normal table view I know I would just use [tableView reloadData]. I have custom delegate methods for assigning labels / rowHeight and some other customizable features. I just need all these delegate methods to be reevaluated after a switch is pressed. Any suggestions? Thanks!!
EDIT:
I thought I was having a brain fart, and for the most part was. I can write my own "reloadData" function in the custom picker class. My only problem is having the picker call the titleForRow delegate method the number of times equal to the number of rows. I know my picker can do it the first time when the view loads. Do I really need to use a FOR loop in my reloadData function or is there something I am missing?
In my reload method I want to call:
[[self delegate] titleForRow:(int) forPicker:self];
Thanks Again!!
I was looking for a quick fix.. but sure enough the right way to do this was to use a FOR loop in a custom reload function for my class. The reload code is essentially the same as the initialization code, just removing subviews and repopulating.

Resources