UIPickerView with 2 NSMutableArrays? - iphone

I have a core data based app I am working on, that uses an EditingViewController to control a number of different UI elements that get input from the user which describe attributes of an object that is subsequently stored within my app. The EditingViewController.xib file has such elements as a datePicker, textField, numberSlider, and where my problem is coming from a UIPickerView, that are all controlled within one view using .hidden = YES/NO; expressions. My problem is that I need to populate a UIPickerView in two seperate screens, that need to have two different NSMutableArray sources. Inside my viewDidLoad method I setup my first NSMutableArray:
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"Greenland"];
[listOfItems addObject:#"Switzerland"];
[listOfItems addObject:#"Norway"];
[listOfItems addObject:#"New Zealand"];
[listOfItems addObject:#"Greece"];
[listOfItems addObject:#"Rome"];
[listOfItems addObject:#"Ireland"];
And after that, I populate my UIPickerView *picker with this code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
return 1;
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
label.text= [listOfItems objectAtIndex:row];// The label is simply setup to show where the picker selector is at
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
return 8;//[listOfItems count];
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
return [listOfItems objectAtIndex:row];
}
And that works fine for the first attribute and array. So then after that, when a different uitableviewcell is selected, the picker is hidden picker.hidden = YES; and I need to have another picker, picker2 show up with a different array of information. But when I try and duplicate the process, by setting up a whole new picker, calling it picker2, and trying to populate it with a different NSMutableArray that I created right next to the first one (again, all because in my EditingViewController it's all part of the same view, just calling different UI elements depending on the view) I can't get picker2 to populate with the new array. I don't know how to set it up so that my two different arrays can be populated. Do I need two picker views? Can it be done with just one? What is the proper syntax in the - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; method to make the two arrays viewable seperately? I hope someone can help me out on this, thank you in advance!

You are asking to use the same object as the delegate for two pickers.
In each of the delegate methods, the picker is provided as the first argument for this purpose. You can check which picker is passed in and return the appropriate data for that picker.
For example, if you add a property for the first picker named "pickerOne" and you second mutable array is called "arrayTwo", the delegate methods would look like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
return 1; // assuming both pickers only have 1 component
}
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
// The label is simply setup to show where the picker selector is at
if (picker == self.pickerOne) {
label.text= [listOfItems objectAtIndex:row];
} else {
label.text= [arrayTwo objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
if (picker == self.pickerOne) {
return [listOfItems count];
} else {
return [arrayTwo count];
}
}
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
if (picker == self.pickerOne) {
return [listOfItems objectAtIndex:row];
} else {
return [arrayTwo objectAtIndex:row];
}
}
Also, you can populate your array like this (if you just have a static list of strings, the array does not need to be mutable):
listOfItems = [[NSArray alloc] initWithObjects:#"Baptisms",#"Greenland",#"Switzerland",#"Norway",#"New Zealand",#"Greece",#"Rome",#"Ireland",nil];

Related

Can't solve the double picker with images

I'm trying to do a double picker, one part of text and the other with images. But the code gives me an error: Thread 1: Program received signal: "EXC_BAD_ACCESS". I can't see the trouble. Here is the code, Array content Images and Array2 content text. Thanks.
#synthesize Array, picker, Array2;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIImage *one = [UIImage imageNamed:#"6-12AM.png"];
UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"Array"];
[self setValue:Array1 forKey:fieldName];
Array2 = [[NSArray alloc] initWithObjects:#"Hello", #"trouble", nil];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;// giving number of components in PickerView
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array2 count];// counting the number of rows in each component
}
#pragma mark Picker Delegate Methods
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
{
if (component == 1) {
return [self.Array objectAtIndex:row];
}
} //In this line is where the error occurs
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
if (component == 0) {
return [self.Array2 objectAtIndex:row];
}
}
Your error is due to the fact that you do not offer an alternative to the if statement. A non-void function must return something in every case, so you need to provide an else statement in each one of your two functions that returns a value (like nil).
I dont see you allocating or initializing Array. Hence the bad access. Did you mean to use Array1?
Also several things:
Memory allocated several places never deallocated. Leaks all over
Variable names should start with small caps like array1, array2
[self setValue:Array1 forKey:fieldName]; //what is this doing?

picker not loading data

I have a detail view with several textfields, next to one of them i have a button a user can click to pop up a single component picker. the picker is hidden till the user clicks the button. just to make sure that the basics work, I have a simple array assignd to the pickerdata. the button to show the picker works but the data doesn't show up, here is the code
- (IBAction)ButtonPressed
{
NSArray *array = [[NSArray alloc] initWithObjects:#"data", #"data 1", nil];
self.pickerData = array;
vPicker.hidden = NO;
selectButton.hidden = NO;
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
Include UIPickerview datasource and delegate in the interface of your class..then set vpicker.datasource = self and vpicker.delegate= self;

XCode: How to fill UIPickerView in code directly with content?

How do I do that? i want to fill it with values like EURO, USD, POUND and so on and paste the value into a textfield when i tap on the corresponding row.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc]
initWithFrame:CGRectMake(0, 244, 320, 270)];
[self.view addSubview:picker];
return NO;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myTextField.text= [PickerArray objectAtIndex:row];
}
You should implement in your delegate method -(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component that will return titles #"EURO", #"USD", #"POUND", #"RUB" for your rows.
For example,
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch(component)
{
case 0:
return #"EURO";
case 1:
return #"USD";
case 2:
return #"POUND";
case 3:
return #"RUB";
}
return #"";
}
write code in your delegate method
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return string;
}
In Array add like this #"one", #"two", #"three", #"four"
As others have already written you have to add the above mentiond delegate method to fill up your PickerView with items from an array. For writing the value into a textfield, you can do this in 2 ways. You can either do write a pickerView didSelectRow: delegate method like this:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myTextField.text= [myArray objectAtIndex:row];
}
This will update the textField each time you tap on a row. The other way is that you pull the value of the pickerView on a button press or on some other action. Similar to
- (void)someAction:(id)sender
{
NSInteger row = [myPicker selectedRowInComponent:0];
myTextField.text = [myArray objectAtIndex:row];
}

UIpickerView populating multiple uitextFields in the same ScrollView

Hi I am developing an iphone app where I have populated a uipickerview and it works great with one uitextfield. But I have 6 uitextfield I need to populate and I am using the same uipickerview for each UItextfield.
I have working the Ipickerview loaded with array objects and it pops up when each field is touched. The problem is with the code below the UItextfields share the same data from the picker.
I can not figure out how to code so each field gets it own data from the UIPickerView row.
What Am I doing wrong? Any coding suggestions?
Thanks
#implementation BestViewController
-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView
{ return 1; }
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
{return [list count]; }
-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component
{return [list objectAtIndex:row]; }
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
uitextfield1.text = [list objectAtIndex:row];
utitextfield2.text = [list objectAtIndex:row];
}
- (void)viewDidLoad {
[super viewDidLoad];
uitextfield1.inputView = pickerView;
uitextfield2.inputView = pickerView;
list = [[NSMutableArray alloc] init];
[list addObject:#"a"];
[list addObject:#"b"];
[list addObject:#"c"];
[list addObject:#"d"];
}
You need to get hold of the current first responder and set its text property rather than explicitly setting a particular text field.
So,
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
// textFields is an NSArray holding each of the textfields that are using the picker as an input view
for (UITextField textField in textFields)
{
if ([textField isFirstResponder])
{
textField.text = [list objectAtIndex:row];
break;
}
}
}
There may be a more elegant way to find the current first responder, this is off the top of my head. textFields could possibly be an IBOutletCollection, but I haven't used those myself so I can't speak with much authority.
Here is how I would do it:
First, define several different pickers. Of course you use the same UIPickerView but you change one property that helps you distinguish them. There is different data for (almost) each text field. One convenient property that Apple designed exactly for this purpose is the tag, an arbitrary integer available to every UIView. You can assign the same tags to the UITextFields.
For example:
#define kFirstTextField 101
#define kSecondTextField 102
#define kThirdTextField 103
//... etc
In the method when the text field is touched:
[myPickerView setHidden:NO]; // or however you show the picker
[myPickerView setTag:textField.tag];
[myPickerView reloadAllComponents];
In the data methods of the picker:
-(NSString *)pickerView:(UIPickerView *)thePickerview
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (thePickerView.tag==kFirstTextField)
{ return [list1 count]; }
if (thePickerView.tag==kSecondTextField)
{ return [anotherOrTheSameList count]; }
// of course, you can also use a switch statement
return [defaultList count];
}
Do something similar in the titleForRow: method.
Finally, when something is picked, discriminate again by tag:
-(void) pickerView:(UIPickerView *)thePickerview
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];
// this assumes the UIPickerView is the subview of the same view as the UITextFields
if (thePickerview.tag==kFirstTextField)
{ field.text = [list1 objectAtIndex:row]; }
if (thePickerview.tag==kSecondTextField)
{ field.text = [anotherOrTheSameList objectAtIndex:row]; }
// etc.
// alternatively:
field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
}

Using a UISegmentedControl to switch between multiple arrays using a uipickerview

I am working on a core data using app and one of the views is EditingViewController, which acts as the controller for a number of ui elements which describe the attributes of objects. Inside the EditingViewController, all of my ui elements are being called and hidden with the .hidden = YES/NO; operation. One of my ui elements is a uipickerview. Currently, there is one array for each of two different views. Meaning that if they clicked on the first name field, array1 would load in picker1, and they would pick from it, and then if they clicked on last name, array2 would load in picker2, and life is good. Here is my code I am using to make this work up until now:
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
return [array1 objectAtIndex:row];
} else {
return [array2 objectAtIndex:row];
}
}
But! array1 is so big that I would like to add a UISegmentedControl to picker1 so that it can sort array1's info a little better. I know that UISegmentedControl is just a pretty set of buttons, requiring IBActions to be linked and stuff, but how could I implement it so that in one of my views, the first one, just picker1 and array1 are managed by the UISegmentedControl? Is it possible to make it so that it is just a smooth sorting process? I figure I will break down array1 into other arrays based on the conditions I set in my UISegmentedControl, how do I link those so they look and work good? Any help on this topic as always is greatly appreciated! Thanks
Use the selectedSegmentIndex property.
For example:
enum {
SEGMENT1,
SEGMENT2,
SEGMENT3
};
- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 objectAtIndex:row];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 objectAtIndex:row];
}
} else {
return [array2 objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (picker1 == self.picker) {
if (segmentedControl.selectedSegmentIndex == SEGMENT1) {
return [array1sub1 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT2) {
return [array1sub2 count];
} else if (segmentedControl.selectedSegmentIndex == SEGMENT3) {
return [array1sub3 count];
}
} else {
return [array2 count];
}
}
Also, you will want to reload the picker when the segmented control changes, so you should link the segmented control's value-changed event with a method like this:
- (IBAction)handleValueChanged
{
[self.picker reloadAllComponents];
}