Can't solve the double picker with images - iphone

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?

Related

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];
}

My Picker shows question marks, but still sends the proper info to my Label

I made a simple picker that displays the information chose in a label on the same page. The picker is just showing ? ? ?, but when i scroll over the ? it shows the correct things chosen in the label. How do I get it to also show the words in the pickerwheel itself?
Without seeing your code, it is difficult to track down. How are you setting the information in the Label and the Picker?
If you have an array of strings which are also displayed in the Picker, then this is easy:
In the .h file, use:
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
NSArray * arrayOfString;
UILabel * theLabel;
UIPicker * thePicker;
}
In the .m file, use:
#pragma mark -
#pragma mark View
- (void)viewDidLoad;{
[super viewDidLoad];
arrayOfStrings = [[NSArray alloc] initWithObjects:#"1", #"2", #"etc", nil];
}
#pragma mark -
#pragma mark Picker Delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView; {
return [arrayOfStrings count];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component; {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
return [arrayOfStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; {
theLabel.text = [arrayOfStrings objectAtIndex:row];
}
You then hook everything up in the nib, and this should fix your problem. Hope this helps!

Array disappears

I'm in a view that has a UIPickerView. Here is where I'm making my sortedArray.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *studentNames = [[NSMutableArray alloc] init];
for (Student *student in course.students)
[studentNames addObject:student.name];
sortedArray = [studentNames sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
[picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}
I can do an NSLog([sortedArray componentsJoinedByString:#", "]); in these two methods and it works:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
and
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
but when I do that same trace in this method it doesn't work (It crashes):
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
I don't understand why the sortedArray works everywhere but in this one method.
From documentation of sortedArrayUsingSelector::
The new array contains references to the receiver’s elements, not copies of them.
Maybe the original strings are already released?
BTW, I see that you don't release the studentNames - memory leak...

Default Picker Value iphone

Is there a way to set a default value for a picker? I save the last selected row from all the pickers and I want to be able to have the pickers load those saved rows at start up. As of now I have found this code:
[settingsPagePicker selectRow:3 inComponent:0 animated:YES];
It works but only when the user taps the picker. I need it to work when the app is first loaded. If I put this code at viewDidLoad the app will crash.
Anyone know where the proper place to put this in my code to make it work?
Thank you for your time!
Have you checked your data source for the settingsPagePicker before calling -selectRow:inComponent:animated to make sure there is data available at that index (3 in your sample code)?
How are you loading your data for your data source? You can initialize your data source in viewDidLoad first and then call selectRow once you know there is data available.
UPDATE: Here is what your code should look like or something like it:
- (void)viewDidLoad
{
[super viewDidLoad];
pickerDataSource = [[NSMutableArray alloc] init];
[pickerDataSource addObject:#"Item 01"];
[pickerDataSource addObject:#"Item 02"];
[pickerDataSource addObject:#"Item 03"];
[pickerDataSource addObject:#"Item 04"];
// Might want to move this to -viewWillAppear:animated
[settingsPagePicker selectRow:3 inComponent:0 animated:YES];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView == settingsPagePicker)
{
return [pickerDataSource objectAtIndex:row];
}
return #"";
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == settingsPagePicker)
{
return [pickerDataSource count];
}
return 0;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
Here some methods from my class.
- (void)viewDidLoad {
[super viewDidLoad];
currentNumbersOfComponents = 1;
//[picker selectRow:2 inComponent:0 animated:NO];
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[picker selectRow:2 inComponent:0 animated:YES];
}
#pragma mark picker view data source methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return currentNumbersOfComponents;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return 5;
}
picker.dataSource is self

UIPickerView with 2 NSMutableArrays?

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];