ios : display the array value in uitextfield? - ios

display the key value in textfield.
this is how I am fetching data:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (txtviolation.editing == YES)
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;[![enter image description here][1]][1]
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
txtviolation.inputView = pickerV;
txtviolation.inputAccessoryView = toolBar;
flag=1;
}
else if (txttype.editing == YES)
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
txttype.inputView = pickerV;
txttype.inputAccessoryView = toolBar;
flag=2;
}
}
this is how display data:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (flag==1)
{
return ViolationA.count;
}
else if (flag==2)
{
return VTypeA.count;
}
return 0;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (flag==1)
{
return [NSString stringWithFormat:#"%#", [ViolationA objectAtIndex:row]];
}
else if (flag==2)
{
return [NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (flag==1)
{
// [txtviolation setText:[[ViolationA objectAtIndex:row] forKeyPath:#"violation_name"]];
txtviolation.text =[NSString stringWithFormat:#"%#", [ViolationA objectAtIndex:row]];
}
else if (flag==2)
{
txttype.text = [NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
}
}
I am not able to bind:
click on textfield then open the uipicker view but not display title name only display the '{' sign.
then roll it to set value of array key and value also but I am only set the value.
uitextfield setvalue:
{ "violation_name" = "Street Light"; }
and
uipickerview display:
{
{
{
then to select any value to set in text field.
uipickerview perform to select the text field not use any button

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (flag==0)
{
return ViolationA.count;
}
else if (flag==1)
{
return VTypeA.count;
}
return 0;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (flag==0)
{
return [[ViolationA objectAtIndex:row] valueForKey:#"violation_name"];
}
else if (flag==1)
{
return [[VTypeA objectAtIndex:row] valueForKey:#"priorirty_name"];
//[NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
//violation_name
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (flag==0)
{
txtviolation.text =[[ViolationA objectAtIndex:row] valueForKey:#"violation_name"];
//[NSString stringWithFormat:#"%#",[ViolationA objectAtIndex:row]];
}
else if (flag==1)
{
txttype.text = [[VTypeA objectAtIndex:row] valueForKey:#"priorirty_name"];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
if (txtviolation.editing == YES)
{
txtviolation.inputView = pickerV;
txtviolation.inputAccessoryView = toolBar;
flag=0;
}
else if (txttype.editing == YES)
{
txttype.inputView = pickerV;
txttype.inputAccessoryView = toolBar;
flag=1;
}
}

Related

Objective C how to re add the deleted items in NSMutableArray

In my code, three text fields are there for picker views. For example in the first text field, the user selects A+ blood group, in the second text field we don't want to display A+ blood group. Remaining items need to display. Text field 3 also same. For example, in text field 2 the user selects B+ blood group, in text field 3 B+ and A+ blood group needs to remove in array list. For example the user again selects the first text field and reselects another blood group like A- ve group re add the A+ blood group in array list. The same thing in text field2 and text field3. I already completed the one task, i.e removing items from array. The pending task is re adding the items in array.
- (void)viewDidLoad {
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc] initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if (!isBloodGroupFieldSelected) {
return 1;
}
else if (!isGenderGroupFieldSelected) {
return 1;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isGenderGroupFieldSelected) {
return [dataArray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
return dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
return dataArray[row];
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
txtField1.text = dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1) {
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
- (void)done:(id)sender {
[myPickerView selectRow:0 inComponent:0 animated:YES]; // On 27/04/2020
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}

objective c how to remove the item in second and third text field based on select the first text field item

I have one UIPickerview. I have a json response. I have a three text fields. If user select the first text field display picker view then user select the particular item. And second and third text field also same. My requirement Is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same.
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2){
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
-(void)done:(id)sender
{
[myPickerView selectRow:0 inComponent:0 animated:YES]; // On 27/04/2020
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}

Bar button methods in toolbar are not calling

I have created a bar button items programatically. But the respective methods of cancel and done are not firing.
I am adding the picker view as input view for textfield programatically. On top of picker view i placed a toolbar with done and cancel buttons. Toolbar is the subview of pickerview.
Here is my code :
(void)awakeFromNib
{
contactAgeRange = [[NSMutableArray alloc]init];
for (int i = 18; i <= 100; i++)
{
[contactAgeRange addObject:[NSNumber numberWithInt:i]];
}
personGender = [[NSMutableArray alloc]initWithObjects:#"Male", #"Female", nil];
[self initializePickerView];
self.cellTextField.userInteractionEnabled = NO;
self.cellTextField.delegate = self;
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:#selector(doneButtonPressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:#selector(cancelButtonPressed)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, space, doneButton, nil]];
[self.pickerView addSubview:toolBar];
}
-(void) configureCell
{
switch (self.cellType) {
case ContactCellTypeAge:
self.cellTextField.inputView = self.pickerView;
break;
case ContactCellTypeGender:
self.cellTextField.inputView = self.pickerView;
break;
}
}
-(void)initializePickerView
{
self.pickerView = [[UIPickerView alloc]init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (self.cellType)
{
case ContactCellTypeAge:
return [contactAgeRange count];
break;
case ContactCellTypeGender:
return [personGender count];
break;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
NSString *age = [[contactAgeRange objectAtIndex:row] stringValue];
return age;
}
else if (self.cellType == ContactCellTypeGender)
{
NSString *gender = [personGender objectAtIndex:row];
return gender;
}
return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
pickerRowText = [[contactAgeRange objectAtIndex:row] stringValue];
//self.cellTextField.text = pickerRowText;
}
else if (self.cellType == ContactCellTypeGender)
{
pickerRowText = [personGender objectAtIndex:row];
//self.cellTextField.text = pickerRowText;
//[self.cellTextField endEditing:YES];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.setNextResponderActionBlock)
{
self.setNextResponderActionBlock();
}
return YES;
}
-(void) cancelButtonPressed
{
[self.cellTextField resignFirstResponder];
}
-(void) doneButtonPressed : (id)sender
{
if (self.cellType == ContactCellTypeAge)
{
self.cellTextField.text = pickerRowText;
}
if (self.cellType == ContactCellTypeGender)
{
self.cellTextField.text = pickerRowText;
}
}
Please help me. Thanks in advance
configureCell in this Method add self.cellTextField.inputAccessoryView = toolbar;

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.

Resources