I want the same style as in UIDatePicker, but in stead of choosing a date I want to choose a number. Is this possible?
Use for that UIPickerview and it's datasource and delegates methods as for UITableView.
See this post
Related
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
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.
I have a UIDatePicker in a view whose controller is in a UINavigationController. I'm registering changes in the selected date like this:
[self.datePicker addTarget:self.alarm action:#selector(timeChanged:) forControlEvents:UIControlEventValueChanged];
It works well, except if the user presses the back button in the UINavigationController before the date picker has completely settled. In that case, the timeChanged: method isn't being called, and so I'm not getting the new date.
Nothing is being released when I press the back button, it's just that the view controller is being popped off. (A reference to it is maintained).
I've found solutions to this problem with UIPickerView that involve implementing delegate methods for the picker view (eg: UIPickerView: Get row value while spinning?). But unless I'm very mistaken, that's not an option with a UIDatePicker. If possible, I'd like to stick with the UIDatePicker rather than a custom UIPickerView. Is there a way to do this? It doesn't have to get the selected row perfectly (within a row or two is fine), but the closer the better.
EDIT: This is for a jailbreak app, so private APIs are fine, if I can figure out what they are.
As for the current APIs, there's no way of detecting whether a UIDatePicker is spinning.
This is because UIDatePicker is not a subclass of UIPickerView and it manages a UIPickerView internally.
I'm afraid the best solution here is to use a custom 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.
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.