I have added a UI Picker in my code programmatically:
UIPickerView *categoryList = [[UIPickerView alloc] initWithFrame:CGRectMake(10, 200, 300, 200)];
categoryList.showsSelectionIndicator = YES;
categoryList.hidden = NO;
categoryList.delegate = self;
[cell.contentView addSubview:categoryList];
But, its not displaying when i run it on simulator.
set to textField inputView
Don't add subview.
You need to implement UIPickerViewDelegate methods for something to appear.
Delegate methods.
#Required
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
}
Also UIPickerViewDelegate methods for presenting your view or string in PickerView as one below :-
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
UPDATE:-
Change your pickerView frame as below
UIPickerView *categoryList = [[UIPickerView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, cell.contentView.frame.origin.y+5, 300, 200)];
Related
I would like the user to be able to change the title of a button this is a hardcoded example of what I intend to do:
[myButton setTitle:#"B" forState:UIControlStateNormal];
However, I would rather prompt the user with a UIPicker, and allow the user to choose from many options in order to change the title.
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0;
if ([sender.view isKindOfClass:[UIButton class]]) {
NSLog(#"user has clicked the button%#", sender.view);
UIButton *myButton = (UIButton *)sender.view;
UIPickerView *picker = [[UIPickerView alloc] init];
myButton.titleLabel = picker;
}
}
At this point I see an error *assignment to readonly property Is it possible to use the UIPickerView in conjunction with the setTitle property of UIButton? If so how is this accomplished?
The error is obvious:
titleLabel is a UILabel property of the UIButton class, and it is read-only (you cannot set it to another value). You need to use its label's text property instead:
myButton.titleLabel.text = #"some text";
and not:
myButton.titleLabel = picker;
or
myButton.titleLabel.text = picker;
because picker is not an NSString object.
Let me explain in a minute how to implement this...
In your View Controller class, create a property (I recommend to do it in the private interface), and also implement the UIPickerViewDataSource, UIPickerViewDelegate protocols:
#interface PPMyViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (nonatomic, strong) UIPickerView *pickerView;
#end
Now, use lazy instantiation for the pickerView property:
- (UIPickerView *)pickerView
{
if (!_pickerView)
{
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
_pickerView.dataSource = self;
_pickerView.delegate = self;
}
return _pickerView;
}
Make sure you add to the main view, you can do it in the viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.pickerView];
}
And implement most of the 2 protocols methods:
#pragma mark - UIPickerViewDataSource Methods
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
#pragma mark - UIPickerViewDelegate Methods
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"row %d", row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"Row %d selected", row);
// myButton.titleLabel.text = #"text you need depending on row selection";
}
Notice that my code will make the UIPickerView visible at the bottom all the times. You may hide/unhide it as needed. Or better, use animation to position it off-screen instead of hiding.
You cannot do this like you attempt to. What you need to do is listen to the user's tap of the button, and then present a picker to the user.
There are open source controls that can simplify this, such as ActionSheetPicker.
Let me preface by saying that I am completely new to programming. I have also done a bunch of searching on this issue and haven't found an answer that seems to address exactly what I am doing.
The result I am looking for is a text field that can be populated by the user by using a pickerView. After putting a UITextView box in the .xib file I am not sure how make it call a pickerView when tapped and then populate from that picker view.
Thank you for any help given. Let me know if I need to clarify more.
Create a UIPickerView and set it as the inputView property of your UITextView. Then, register your class for the dataSource/delegate of the UIPickerView to populate it and know when it has changed selection. Below is a simple solution for a picker with a single component.
SomeViewController.h:
#interface SomeViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
UIPickerView *picker;
NSArray *dataSource;
}
SomeViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;
myTextView.inputView = picker;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataSource count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [dataSource objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[myTextView setText:[dataSource objectAtIndex:row]];
}
I am trying to create a PickerView view programatically.
I have followed all of the necessary steps however the picker just simply appears and then disappears straight away. No data can be seen.
I am setting the delegate and data source and I have implemented the necessary methods.
Here is my code:
in .h
#interface NJATimeEntryViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, strong) UIPickerView *customerPicker;
#property (nonatomic, strong) NSArray *pickerTitles;
#end
in .m
- (void)viewDidLoad
{
[super viewDidLoad];
self.pickerTitles = #[#"ONE", #"TWO", #"THREE"];
self.customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
self.customerPicker.dataSource = self;
self.customerPicker.delegate = self;
[self.view addSubview:self.customerPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.pickerTitles.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[self pickerTitles] objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"Selected Row %d", row);
}
All help is appreciated thanks.
FIXED:
All i did was set the background color of the view controllers view and now it works. I see this as a bug as no where is it stated that the background color has to be set.
Set Some Background color ( except black )of your view, then try,
self.view.backgroundColor=[UIColor yellowColor];
I am using UIPickerView with UITextField. It is working when I use only one UITextField.
What I want is to use multiple TextField for one dynamic pickerview.
- (void)viewDidLoad
{
[super viewDidLoad];
picker = [[UIPickerView alloc] init];
picker.dataSource = self;
picker.delegate = self;
self.transitTF.inputView = picker;
self.theData = #[#"one",#"two",#"three",#"four"];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.theData.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.theData[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.transitTF.text = self.theData[row];
[self.transitTF resignFirstResponder];
}
This is what I use with one textField only.
Any help will be appreciated. Thanks.
Add tag property for each UITextField. In this case self.pickerView.inputView.tag you may use to find which textfield is using in current moment and you may fill picker with correct values
I am new to iOs and trying to add the picker UI element dynamically to the UI.
In the class - UIPickerView , i don't see any methods for adding the picker to the view.
Has anyone tried doing this?
Thanks a lot.
As with other you need to alloc+initWithFrame.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 0, 0)];
Then add it to your target view.
[yourView addSubview:pickerView];
Also you need to set it delegate and datasource, which will show all your elements in the picker view.
pickerView.dataSource=self;
pickerView.delegate=self;
And all other customization as :
pickerView.showsSelectionIndicator=YES;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
You adding the picker to the view just like and UIView you want to add -> addSubview method.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,200,320,200,)]];
pickerView.delegate = self;
[self.view addSubview:pickerView];
Don't forget to set it's delegate and implement it in order to achieve it's functionality.
First alloc memory to your picker view.
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 265, 320, 216)];
and then set delegate and datasource.
pickerView.dataSource=self;
pickerView.delegate=self;
after that add picker view to your main view.
[self.view addSubview:pickerView];
after that you should implement pickerview's method .
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [array objectAtIndex:row];
}
i hope this will work fine.
i don't see any methods for adding the picker to the view
A UIPickerView is a subclass of UIView, so you use the same methods that you'd use to add any subview to a view, namely -addSubview:, like this:
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:theFrame];
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
I'm assuming here that the code is in a view controller that's acting as both the delegate and data source for the picker.