在UIPickerViewDelegate方法的末尾显示警告消息

时间:2011-08-11 11:49:37

标签: iphone objective-c xcode

我在我的应用程序中创建了一个pickerview,并为我的.h文件代码创建了两个组件,如下所示

@interface tweetViewController : UIViewController<UIPickerViewDataSource ,UIPickerViewDelegate> {

    NSArray *activities;
    NSArray *feelings;

}
@property(nonatomic,retain) NSArray *activities;
@property(nonatomic,retain) NSArray *feelings;
@end

之后在我的.m文件中我已经为UIPickerViewDataSource和UIPickerViewDelegate实现了强制方法,但是UIPickerViewDatasource的两个编译方法都能正常工作 它的代码是

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

但是UIPickerViewDelegate的方法在方法结束时显示警告

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

}

告诉我为什么会发出警告????

2 个答案:

答案 0 :(得分:1)

警告“控件可能会到达非void函数的结尾..”意味着该方法没有返回任何内容,因为该方法具有一些返回值。

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

如上所述修改您的代码,警告将不再令您烦恼。

答案 1 :(得分:0)

//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 

对于titleForRow方法,你必须在默认情况下以atleast返回nsstring

所以为某些字符串指定defaust case

相关问题