Black inside the pickerview - ios

It is all black in the UIPickerView. I do not know what is happening. I do not have warnings and errors and when I try to bring up the picker view it just shows black. Did I do something wrong and is there a possibility that I have forgoten something.
This is my code.
#synthesize stillImageOutput, imagePreview, captureImage, cameraSwitch, pickerViewContainer;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if (component == genre)
return [arraygenre count];
return 0;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == genre)
return [arraygenre objectAtIndex:row];
return 0;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)saveButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame = CGRectMake(0, 307, 320, 261);
[UIView commitAnimations];
}
- (IBAction)closeButton:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
pickerViewContainer.frame = CGRectMake(0, 800, 320, 261);
[UIView commitAnimations];
}
Update
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FrontCamera = NO;
cameraSwitch.selectedSegmentIndex = 1;
captureImage.hidden = YES;
arraygenre = [[NSMutableArray alloc] init];
[arraygenre addObject:#"blue"];
[arraygenre addObject:#"yellow"];
[arraygenre addObject:#"white"];
[arraygenre addObject:#"red"];
[arraygenre addObject:#"purple"];
[arraygenre addObject:#"Others"];
pickerViewContainer.frame = CGRectMake(0, 800, 320, 261);
}
- (void)viewDidAppear:(BOOL)animated {
[self initializeCamera];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//fetch Category Name from the array used to fill the Picker View
NSString *categoryName= [arraygenre objectAtIndex:row];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:categoryName];
NSFileManager *fileManager=[[NSFileManager alloc]init];
[fileManager createDirectoryAtPath:fPath withIntermediateDirectories:YES attributes:nil error:nil];
UIImage *image = captureImage.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:fPath atomically:YES];
}
In my .h file i have
#define genre 0
#interface CameraSave : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate,UIImagePickerControllerDelegate> {
BOOL FrontCamera;
BOOL haveImage;
IBOutlet UIPickerView *SaveTopicker;
NSMutableArray *arraygenre;
}

have you allocate & intialize pickerview?? and is delegate is set?
try this in viewDidLoad
SaveTopicker.delegate = self;
SaveTopicker.showsSelectionIndicator = YES;
[self.view addSubview:SaveTopicker];
for more reference check this Tutorial1, Tutorial 2

- (void)viewDidLoad
{
[super viewDidLoad];
array=[[NSMutableArray alloc]initWithObjects:#"Reading",#"Music",#"Cricket",#"Movie",#"Travelling",nil];
[hobbyPicker selectRow:1 inComponent:0 animated:NO];
}
-(void)buttonHobby:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
hobbyPicker.frame = CGRectMake(0, 244, 320, 216);
pickerToolbar.frame = CGRectMake(0,200, 320, 44);
[UIView commitAnimations];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSUInteger numRows = 5;
return numRows;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [array objectAtIndex:row];
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)componen
{
CGFloat componentWidth = 0.0;
componentWidth = 135.0;
return componentWidth;
}
Try this code acording to your need... hope this wprks for u..

Related

UIPickerView selectedRowInComponent:0 always return zero first when I try to set data in second component

I'm new to iOS programming.
Recently, I'm trying to make a UIPickerView as an inputView in UITextfield.
The data in UIPickerView is about all the iOS built-in fonts.
So I want to make two components in UIPickerView: the first is familyType, and the second is all the fonts in that familyType.
I simulate the code from this answer, but I meet some problem I can't solve. Any help is welcome!
My question is here:
Why rowOneSelected in this function always get 0 first, even I use selectedRowInComponent in advance?
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
NSLog(#"%ld", (long)rowOneSelected); // I use this to debug, and there is a main question: why every time it logs 0 first?
return temp.font.count;
}
}
All my relative code is here:
In ViewController.h:
#import <UIKit/UIKit.h>
#import "MenuLayerTwoPlusThree.h"
#interface ViewController : UIViewController
#property MenuLayerTwoPlusThree *layerTwoPlusThree;
- (void)createLayerTwoPlusThree;
#end
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self createLayerTwoPlusThree];
}
- (void)createLayerTwoPlusThree
{
_layerTwoPlusThree = [MenuLayerTwoPlusThree alloc];
[_layerTwoPlusThree createFontArray];
[_layerTwoPlusThree createSelectPanel];
}
In FontType.h:
#ifndef FontType_h
#define FontType_h
#import <Foundation/Foundation.h>
#interface FontType : NSObject
#property NSString *familyName;
#property NSMutableArray *font;
#end
#endif /* FontType_h */
In FontType.m:
#import <Foundation/Foundation.h>
#import "FontType.h"
#implementation FontType
#synthesize familyName;
#synthesize font;
#end
In MenuLayerTwoPlusThree.h:
#ifndef MenuLayerTwoPlusThree_h
#define MenuLayerTwoPlusThree_h
#import <UIKit/UIKit.h>
#import "FontType.h"
#interface MenuLayerTwoPlusThree : NSObject<UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
#property UITextField *textFieldFont;
#property NSMutableArray *fontTypeArray;
#property UIPickerView *pickerFont;
#property UIBarButtonItem *doneButton;
#property UIBarButtonItem *spaceButton;
#property UIBarButtonItem *cancelButton;
#property UIToolbar *toolBar;
#property NSArray *toolBarItems;
#property NSInteger familyType;
#property NSInteger fontType;
#property NSString *fontName;
- (void)createFontArray;
- (IBAction)pickerViewButtonClicked:(id)sender;
#end
In MenuLayerTwoPlusThree.m
- (void)createFontArray
{
_fontTypeArray = [[NSMutableArray alloc]initWithCapacity:80];
int number = 0;
for(NSString* family in [UIFont familyNames])
{
//NSLog(#"%#", family);
//number++;
FontType *temp = [[FontType alloc]init];
temp.familyName = family;
temp.font = [[NSMutableArray alloc]init];
int flag = 0;
for(NSString* name in [UIFont fontNamesForFamilyName:family])
{
//NSLog(#" %#", name);
//number++;
flag++;
[temp.font addObject:name];
}
// add Heiti SC, Heiti TC, Telugu Sangam MN, and Bangla Sangam MN to font array
if(flag == 0)
{
[temp.font addObject:family];
}
[_fontTypeArray addObject:temp];
}
// print all fonts test
for(FontType *x in _fontTypeArray)
{
number++;
NSLog(#"%#", x.familyName);
for(NSString *y in x.font)
{
//number++;
NSLog(#"\t%#", y);
}
}
NSLog(#"//////////////////////////////");
NSLog(#"%d", number);
}
- (void)createSelectPanel
{
[self createSelectPanelForPancel1];
[self createSelectPanelForPancel2];
[self createSelectPanelForFont];
[self createSelectPanelForShape];
[self createSelectPanelForEraser];
}
- (void)createSelectPanelForFont
{
_textFieldFont = [[UITextField alloc]initWithFrame:CGRectMake(19, 148, 150, 12)];
[_textFieldFont setBackground:[UIImage imageNamed:#"font-type-bar.png"]];
_textFieldFont.rightViewMode = UITextFieldViewModeAlways;
_textFieldFont.delegate = self;
[_textFieldFont setPlaceholder:#"Heiti TC"];
_textFieldFont.font = [_textFieldFont.font fontWithSize:10 * _aspectRatio];
// resize right view image
UIImageView *rightViewImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 12 * _aspectRatio, 12 * _aspectRatio)];
[rightViewImage setImage:[UIImage imageNamed:#"font-type-bar-roll.png"]];
_textFieldFont.rightView = rightViewImage;
_pickerFont = [[UIPickerView alloc]init];
_pickerFont.dataSource = self;
_pickerFont.delegate = self;
_pickerFont.showsSelectionIndicator = YES;
_doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(pickerViewButtonClicked:)];
_spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_cancelButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(pickerViewButtonClicked:)];
_toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
[_toolBar setBarStyle:UIBarStyleDefault];
_toolBarItems = [NSArray arrayWithObjects:_cancelButton, _spaceButton, _doneButton, nil];
[_toolBar setItems:_toolBarItems];
_textFieldFont.inputView = _pickerFont;
_textFieldFont.inputAccessoryView = _toolBar;
if (#available(iOS 9.0, *)) {
_textFieldFont.inputAssistantItem.leadingBarButtonGroups = #[];
} else {
// Fallback on earlier versions
}
if (#available(iOS 9.0, *)) {
_textFieldFont.inputAssistantItem.trailingBarButtonGroups = #[];
} else {
// Fallback on earlier versions
}
// I want to add these codes to select row in advanced to make sure first time NSLog will print 9, but it doesn't work.
// [_pickerFont reloadAllComponents];
// _familyType = 9;
// _fontType = 0;
// _fontName = #"Heiti TC";
// [_pickerFont selectRow:_familyType inComponent:0 animated:YES];
[_selectPanelFontView addSubview:_textFieldFont];
}
And this is the delegate, which I am written in the MenuLayerTwoPlusThree.m:
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
NSLog(#"%ld", (long)rowOneSelected); // I use this to debug, and there is a main question: why every time it logs 0 first?
return temp.font.count;
}
}
// The data to return for the row and component (column) that's being passed in
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
FontType *temp = _fontTypeArray[row];
return temp.familyName;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
return [temp.font objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
[_pickerFont reloadComponent:1];
}
// This block is moved to pickerViewButtonClicked (sender == _doneButton)
// else
// {
// NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
// FontType *temp = _fontTypeArray[rowOneSelected];
// [_textFieldFont setText:temp.font[row]];
// }
}
- (IBAction)pickerViewButtonClicked:(id)sender
{
if(sender == _doneButton)
{
// Save value when I clicked the done button.
_familyType = [_pickerFont selectedRowInComponent:0];
_fontType = [_pickerFont selectedRowInComponent:1];
// NSLog(#"family: %ld", _familyType);
// NSLog(#"font: %ld", _fontType);
FontType *temp = _fontTypeArray[_familyType];
_fontName = temp.font[_fontType];
[_textFieldFont setText:_fontName];
// NSLog(#"font name: %#", _fontName);
[_textFieldFont endEditing:YES];
}
else if(sender == _cancelButton)
{
[_textFieldFont endEditing:YES];
// I want to turn back to the last selected value when I clicked the cancel button.
[_pickerFont reloadAllComponents];
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];
}
}
It really seems that when the UIPickerView appears as an inputView after you have selected row 9 in component 0, the selectedRowInComponent: 0 returns 0 in numberOfRowsInComponent: 1, but then returns 9 in titleForRow: row forComponent: 1.
I don't think you are doing anything wrong, so it looks like a bug in UIKit.
As a workaround, I suggest you don't ask the picker view for the selected row, but track the selected row yourself (initialize your own variable in your view controller and update it in didSelectRow: row inComponent: 0)
Sorry, I forgot to post my solution.
I use _fontTypeIsFirst to see if UIPickerView has been selected or not.
When you roll the picker view, you should set _fontTypeIsFirst to NO and reload component 1, so it can return to row 0 in component 1 as usual.
Here is my code:
Some delegates:
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected;
if (_fontTypeIsFirst == YES)
{
rowOneSelected = _selectedRow;
}
else
{
rowOneSelected = [_pickerFont selectedRowInComponent:0];
}
FontType *temp = _fontTypeArray[rowOneSelected];
return temp.font.count;
}
}
// The data to return for the row and component (column) that's being passed in
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
FontType *temp = _fontTypeArray[row];
return temp.familyName;
}
else
{
NSInteger rowOneSelected;
if (_fontTypeIsFirst == YES)
{
rowOneSelected = _selectedRow;
}
else
{
rowOneSelected = [_pickerFont selectedRowInComponent:0];
}
FontType *temp = _fontTypeArray[rowOneSelected];
return [temp.font objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
_fontTypeIsFirst = NO;
if(component == 0)
{
[_pickerFont reloadComponent:1];
}
}
And this is action of done and cancel button:
- (IBAction)pickerViewButtonClicked:(id)sender
{
if(sender == _doneButton)
{
_selectedRow = [_pickerFont selectedRowInComponent:0];
_familyType = [_pickerFont selectedRowInComponent:0];
_fontType = [_pickerFont selectedRowInComponent:1];
FontType *temp = _fontTypeArray[_familyType];
_fontName = temp.font[_fontType];
[_textFieldFont setText:_fontName];
[_textFieldFont endEditing:YES];
_fontTypeIsFirst = YES;
}
else if(sender == _cancelButton)
{
[_textFieldFont endEditing:YES];
_fontTypeIsFirst = YES;
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];
}
}
And you can add codes like this to select default value of picker view in viewDidLoad or some place:
_fontTypeIsFirst = YES;
_selectedRow = 9;
_familyType = 9;
_fontType = 0;
_fontName = #"Heiti TC";
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];

Populate UIPickerView with images

I'm trying to populate a UIPickerView with images.
Here is where i create programmatically the PickerView and i fill a NSMutableArray with 35 images called 1.png, 2.png ..., 35.png
#interface ShowImagesPVC () <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) NSMutableArray *arrayOfImages;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
[_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
now i've implemented these methods
- (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;
}
The problem is that the UIPicker is created but it's empty, so i tried to NSLog the method - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view and it seems it's never called.
Any idea why?
try this
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayOfImages = [NSMutableArray array];
for(int i = 1; i < 36; i++){
NSString *tmpString = [NSString stringWithFormat:#"%d.png",i];
[_arrayOfImages addObject:[[UIImageView alloc]initWithImage:[UIImage imageNamed:tmpString]]];
}
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(-5, 0, 315, 200)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}

Customize UIPicker-Insert Images

As seen image below, I am trying to customize my picker. I have successfully added images on my picker but not affected all other pickers. I have put my code as well for reference.
- (void)loadView
{
picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 200, 250, 250)];
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(390, 200, 250, 250)];
picker2.delegate = self;
picker2.dataSource = self;
picker2.showsSelectionIndicator = YES;
picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(750, 200, 250, 250)];
picker3.delegate = self;
picker3.dataSource = self;
picker3.showsSelectionIndicator = YES;
self.view = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:picker1];
[self.view addSubview:picker2];
[self.view addSubview:picker3];
UIImage *seven = [UIImage imageNamed:#"seven.png"];
UIImage *bar = [UIImage imageNamed:#"bar.png"];
for(int i=1; i<=4; i++)
{
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView,barView,nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d",i];
[self setValue:imageViewArray forKey:fieldName];
}
}
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array = [self valueForKey:arrayName];
return [array objectAtIndex:row];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
}
#pragma mark UIPickerViewDelegate methods
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d",row];
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 4;
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 2;
}
#end
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSArray *array = nil;
if (pickerView == picker1) {
array = [self valueForKey:#"column1"];
}
else if (pickerView == picker2) {
array = [self valueForKey:#"column2"];
}
else if (pickerView == picker3) {
array = [self valueForKey:#"column3"];
}
return [array objectAtIndex:row];
}

Custom UIPicker

I have been working on the app, that has 3 uipickers. I want to implement images in each uipicker. However as seen in the image attached below, it just affects only one uipicker, not all of them.I would like to know how I could able to custom all uipicker elements shown in the image
- (void)loadView
{
picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(30, 200, 250, 250)];
picker1.delegate = self;
picker1.dataSource = self;
picker1.showsSelectionIndicator = YES;
picker1.tag=1;
picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(390, 200, 250, 250)];
picker2.delegate = self;
picker2.dataSource = self;
picker2.showsSelectionIndicator = YES;
picker2.tag=2;
picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(750, 200, 250, 250)];
picker3.delegate = self;
picker3.dataSource = self;
picker3.showsSelectionIndicator = YES;
picker3.tag=3;
self.view = [[UIView alloc] initWithFrame:CGRectZero];
self.view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:picker1];
[self.view addSubview:picker2];
[self.view addSubview:picker3];
UIImage *seven = [UIImage imageNamed:#"seven.png"];
UIImage *bar = [UIImage imageNamed:#"bar.png"];
for(int i=1; i<=4; i++)
{
UIImageView *sevenView = [[UIImageView alloc] initWithImage:seven];
UIImageView *barView = [[UIImageView alloc] initWithImage:bar];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
sevenView,barView,nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d",i];
[self setValue:imageViewArray forKey:fieldName];
}
}
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
//NSLog(#"%d",component);
NSLog(#"tag: %d",pickerView.tag);
if(pickerView.tag==1)
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array1 = [self valueForKey:arrayName];
return [array1 objectAtIndex:row];
}
else if(pickerView.tag==2)
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array2 = [self valueForKey:arrayName];
return [array2 objectAtIndex:row];
}
else
{ NSString *arrayName = [[NSString alloc] initWithFormat:#"column%d",component+1];
NSArray *array3 = [self valueForKey:arrayName];
return [array3 objectAtIndex:row];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
}
#pragma mark UIPickerViewDelegate methods
- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:#"%d",row];
}
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv
{
return 4;
}
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component
{
return 2;
}
#end
Views can only have one parent. But you are trying to use each view for 3 parents (the 3 pickers). Regardless of the picker, you try to use the same set of image views for component X of each picker. That won't work.
You need three sets of arrays, not just the one set. In other words, you can't share views between the pickers.

Can't set component width in picker view

Am trying to set the width of two components in a picker view. They are of equal width, and I want to make one wider. For this reason, I have used the
(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
method.
My problem is that after I have set the method, I only get one component back instead of two. I have checked to see if I have written the method wrong, but can't find anything wrong.
Any idea what's wrong?
Here is the code:
#import "BIDDependentComponentPickerViewController.h"
#implementation BIDDependentComponentPickerViewController
#synthesize picker;
#synthesize stateZips;
#synthesize states;
#synthesize zips;
- (IBAction)buttonPressed:(id)sender
{
NSInteger stateRow = [picker selectedRowInComponent:kStateComponent];
NSInteger zipRow = [picker selectedRowInComponent:kZipComponent];
NSString *state = [self.states objectAtIndex:stateRow];
NSString *zip = [self.zips objectAtIndex:zipRow];
NSString *title= [[NSString alloc] initWithFormat:#"You selected zip code %#.", zip];
NSString *message = [[NSString alloc] initWithFormat:#"#% is in #%", zip, state];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:#"statedictionary" withExtension:#"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
self.stateZips = dictionary;
NSArray *components = [self.stateZips allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:#selector(compare:)];
self.states = sorted;
NSString *selectedState = [self.states objectAtIndex:0];
NSArray *array = [stateZips objectForKey:selectedState];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.picker = nil;
self.stateZips = nil;
self.states = nil;
self.zips = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == kStateComponent) {
return [self.states count];
return [self.zips count];
}
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == kStateComponent)
return [self.states objectAtIndex:row];
return [self.zips objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kStateComponent)
{
NSString *selectedState = [self.states objectAtIndex:row];
NSArray *array = [stateZips objectForKey:selectedState];
self.zips = array;
[picker selectRow:0 inComponent:kZipComponent animated:YES];
[picker reloadComponent:kZipComponent];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == kZipComponent) {
return 90;
return 200;
}
}
#end
The line:
return 200;
is never reached. It should be outside the IF statement like so:
if (component == kZipComponent) {
return 90.0;
}
return 200.0;
Otherwise, your method does not return a value for any component OTHER than kZipComponent (so the width of other components is 0).

Resources