warning at the end of UIPickerViewDelegate method - iphone

I use pickerview in my application and in .h file i have written code as follows
#interface tweetViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> {
NSArray* activities;
NSArray* feelings;
}
#property (nonatomic,retain) NSArray *activities;
#property (nonatomic,retain)NSArray *fellings;
and i have written code in my.h file for UIPickerviewDatasource as follows which works fine
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==0)
{
return [activities count];
}
else
{
return [feelings count];
}
}
but the following code of UIPickerViewDelegate shows me warning msg at the closing brace of the method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [activities objectAtIndex:row];
case 1:
return [feelings objectAtIndex:row];
}
}
plz suggest me how i can solve the problem...

This is because the compiler is not sure you're going to return something.
Your method does the following:
if case == 0: return [activities objectAtIndex:row];
if case == 1: return [activities objectAtIndex:row];
In praxis, this will always return a value. However, the compiler checks your code fully theoretically and finds out that if case would be any other integer than 0 and 1, your method doesn't return.
Bottom line is that you need to add return nil; after your switch-block =), so your method will always return (nil will be interpreted as #"" in this case).

Related

Setting data for multiple UIPickerView

I have two UIPickerViews With data being pulled from an array though I can't seem to program them separately. Here is the code that I am using for my UIPickerViews:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:#"treatmentName"];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_buttonText.text = [[treatments objectAtIndex:row]valueForKey:#"treatmentName"];
if (![_buttonText.text isEqual: #"Pick a Treatment Name"]) {
_buttonText.textColor = [UIColor blackColor];
}
}
-(NSInteger)nursePicker:(UIPickerView *)nursePicker numberOfRowsInComponent:(NSInteger)component {
return [nurses count];
}
- (NSString *)nursePicker:(UIPickerView *)nursePicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[nurses objectAtIndex:row]valueForKey:#"nurseName"];
}
When I run the code the pickers show the same data
Thanks in advance
Store a reference to your two picker views and use the UIPickerView that is passed as an argument to the datasource methods in order to determine which picker view you are using.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == self.nursePicker) {
return [nurses count];
}
else if (pickerView == self.treatmentPicker) {
return [treatments count];
}
}
Same idea for every datasource method
You should put a BOOL to check the data. Create a BOOL called treatment and when the first picker is available set the BOOL to YES and when the second is available set the BOOL to NO. Then check in your methods to see which picker is their and put in the data.
#interface NotesViewController ()
{
BOOL treatment;
}
- (void)firstPickerComesUP
{
treatment = YES;
}
- (void)secondPickerComesUP
{
treatment = NO;
}

UIPickerView with empty arrays

I've set up a UIPickerView successfully, but I'm wondering about the best way to handle the case when the data is initially empty. For my current iteration I use the following code for the pickerdelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([flash_cards count])
{
return [[flash_cards allKeys] objectAtIndex:row];
}
return #"";
}
where
flash_cards = NSMutableDictionary *
Whenever I try to scroll it creates an error of indexoutofbounds which is only logical. How do I handle the case of an empty array?
EDIT:
Also as part of the code I implemented the following
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponen: (NSInteger)component
{
return [flash_cards count];
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
To solve this problem you should return 1 instead 0 in the method numberOfRowsInComponent from the UIPickerViewDataSource when the array is empty, like the example below:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.myArray count] == 0 ? 1 : [self.myArray count];
}
You should also implement the UIPickerViewDataSource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
You can still use flash_cards (or [flash_cards count]) to derive the return values of these methods; if your data is empty, you can return 0, then later call -reloadAllComponents on your picker view when you get data loaded.

Iphone:Split a string seperated by comma and print each in row of pickerview

I got a string as mild,medium,hot.I split the string with comma as separator.I need to print it in a pickerView too.
I used the following code and got List count as 3 successfully.
NSString *spList=[mdict objectForKey:#"spicinesstype"];
NSArray *list = [spList componentsSeparatedByString:#","];
NSLog(#"List count:%d",[list count]);
return [list count];
But how could I display all 3 items in pickerview
You should set your class as the delegate of the picker view, Then implement these 3 delegate methods for your picker
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
First you need to implement UIPickerViewDelegate and UIPickerViewDataSource by putting them on the end of the #interface line of the header file of your view controller.
Like this: #interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Next you need to set your view controller as the delegate and data source of you picker view. You can either do this in the - (void)viewDidLoad method of your view controller by adding the lines:
myPickerView.delegate = self;
myPickerView.dataSource = self;
Or you can link it up in Interface Builder if you are using that.
Then you need to implement these delegate methods in your View Controller source file.
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [list objectAtIndex:row];
}

Two PickerViews with different values

This is my code for two pickerviews in one view controller. However its not working for me.
#pragma mark UIPickerViewDelegate methods
//PickerViewController.m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
switch ([thePickerView tag]) {
case 1: //purpose picker
return [m_arrPurpose count];
case 2: //second picker
return [m_arrSweep count];
default:
return 0;
break;
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch ([thePickerView tag]) {
case 1: //purpose picker
{
//cost.text = #"Test";
Purpose *prp = [m_arrPurpose objectAtIndex:row];
return [prp m_purposeName];
}
case 2: //second picker
{
OpenActivity *opn = [m_arrSweep objectAtIndex:row];
return [opn m_ahhaName];
}
default:
return #"";
break;
}
}
can any1 help me with this please..
thanks
It sounds like either your tags aren't set correctly or you havent connected the datasource and delegate methods for both pickers.
Add some NSLog statements in the numberOfRowsInComponent and titleForRow: methods.
Include the picker view object and the picker view's tag in the log, e.g.
NSLog(#"Rows in component for %#, tag %d",thePickerView,[thePickerView tag]);
And a different text in the titleForRow.
You should see two different objects - if not, your delegate and datasource are not connected. You should see tags 1 and 2 - if not, your tags are not set properly.
With this problem there was nothing wrong with my code It's just that I forgot to set tag's value to 1 and 2 in XIB.

shows warning msg at the end of the UIPickerViewDelegate method

I have created a pickerview in my application and create two component of it for that in my .h file code is as follows
#interface tweetViewController : UIViewController<UIPickerViewDataSource ,UIPickerViewDelegate> {
NSArray *activities;
NSArray *feelings;
}
#property(nonatomic,retain) NSArray *activities;
#property(nonatomic,retain) NSArray *feelings;
#end
after that in my .m file i have implemented compulsory method for UIPickerViewDataSource and UIPickerViewDelegate but both compulory methods of UIPickerViewDatasource works fine
and its code is
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return [activities count];
}
else
{
return [feelings count];
}
} return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [activities count];
}
else
{
return [feelings count];
}
}
but the method of UIPickerViewDelegate shows warning at the end of the method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [activities objectAtIndex:row];
break;
case 1:
return [feelings objectAtIndex:row];
break;
}
}
tell me why does the warning occur????
The warning "control may reach end of non-void function.." means to say that the method is returning nothing as the method is having some return value.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *strToReturn = #"";
switch (component) {
case 0:
strToReturn = [activities objectAtIndex:row];
break;
case 1:
strToReturn = [feelings objectAtIndex:row];
break;
}
return strToReturn;
}
Modify your code as above and the warning will be no more to irritate you.
//numberOfRowsInComponent must return NSInteger value
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [activities count];//then wont execute feelings count
}//if loop failed then ur feeling count
return [feelings count];
}
//here u can remove else loop with same meaning with your context
for titleForRow method u have to return nsstring in atleast in default case
so specify defaust case for some some string