I have a UIPickerView that works perfectly when placed on the main view. However as soon as I place it into a sub UIView (To help with visual placement of elements) the interaction stops completely. The UIPickerView still receives all data correctly, just no interaction.
If I take the UIPickerView back out of the UIView, using storyboard, it works right away.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add provinces into array
[self.provinceView setHidden:YES];
isVisible = NO;
self.provinceArray = [[NSArray alloc] initWithObjects:#"Alberta",#"British Columbia",#"Manitoba",#"New Brunswick",#"Newfoundland and Labrador",#"Northwest Territories",#"Nova Scotia",#"Nunavut",#"Ontario",#"Prince Edward Island",#"Quebec",#"Saskatchewan",#"Yukon",nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.provinceArray count];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0,(pickerView.frame.size.width-40), 44)];
label.textColor = [UIColor colorWithRed:0.035 green:0.094 blue:0.345 alpha:1.0];
label.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
label.text = [self.provinceArray objectAtIndex:row];
return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
self.provinceLabel.text = [self.provinceArray objectAtIndex:row];
}
Check this post
-->
UIPickerView in UITableView doesn't react to touch in lower half of cell
did u bring the subview to foreground?
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.
I'm having problems while displaying images in a UIPickerView. The images appear and disappear randomly and never show in the "middle" row of the picker. This is the code
#interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSMutableArray *arrayOfImages;
#end
#implementation ShowImagesPVC
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
UIImageView *myIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[myIcon setImage:[UIImage imageNamed:tmpString]];
[_arrayOfImages addObject:myIcon];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [_arrayOfImages objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _arrayOfImages.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 45;
}
#end
This is a pic of the Picker just loaded
and this is a pic of the Picker a bit rolled
There should be the icons seen in the first pic over the middle row, but there is nothing.
The icon size is 64x64, so i tried to make the first one 40x40 (but doesn't change anything because it should be in the middle row in the first pic, but as you can se it's blank)
Instead of making your _arrayOfImages an array of image views, make it an array of images, and then do this in viewForRow:forComponent:reusingView:,
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
iv.image = _arrayOfImages[row];
return iv;
}
I have a UIPickerView filled with an array of tree types. I also have an empty UIImageView below that I would like to change upon the selection of the picker view. I got a label to change with the selection but cannot get images to change. Here is what I have so far
EDIT
EDIT : Now working! Correct Code below
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataArray = [[NSArray alloc]initWithObjects:#"Birch", #"Elm", #"Maple", #"Oak", #"White Pine",#"Willow", nil];
myPicker.delegate = self;
imageArray = [[NSArray alloc]initWithObjects:#"birch_river.png",#"elm_american.png",#"maple_bigtooth.png", #"oak_bur.png", #"white_pine.png", #"oak_willow.png", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [dataArray count];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:row]];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component
{
return [dataArray objectAtIndex:row];
}
#end
What you can do is, have an array filled with the image names (Ex: IMAGES_ARRAY with image names).
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
myLabel.text = [dataArray objectAtIndex:row];
YOUR_IMG_VIEW = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[IMAGES_ARRAY 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];
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
My font picker is now working fine,
all that is missing is 2 extra sections in the PickerView for font size and style (bold, italic and underlined).
I don't know how to approach changing these values.
This is my picker's code :
#import "ViewController.h"
#interface ViewController ()
{
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
fontNames = [NSMutableArray array];
for(NSString *familyName in [UIFont familyNames]) {
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
[fontNames addObject:fontName];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return fontNames.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [fontNames objectAtIndex:row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[UILabel alloc] initWithFrame:frame];
pickerLabel.backgroundColor = [UIColor clearColor];
pickerLabel.textAlignment = NSTextAlignmentLeft;
pickerLabel.text = [NSString stringWithFormat:#"%#",[fontNames objectAtIndex:row]];
pickerLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:15];
return pickerLabel;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
_fontLabel.text = [fontNames objectAtIndex:row];
_fontLabel.font = [UIFont fontWithName:[fontNames objectAtIndex:row] size:15];
}
#end
You get the fonts using this loop:
NSMutableArray *fontNames = [NSMutableArray array];
for(NSString *familyName in [UIFont familyNames]) {
for(NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
[fontNames addObject:fontName];
}
}
How you present the picker depends on your UI layout. You might use an UITableView in an UIPopoverController (just google it, you'll find TONS of quite good tutorials) or an UIPickerView.