iOS多选择器视图“;分析问题:预期表达式;

iOS多选择器视图“;分析问题:预期表达式;,ios,uipickerview,Ios,Uipickerview,我在一个视图控件中有多个PickerView。它适用于2个选择器视图,但当我添加更多视图时,在以下代码中会出现7个“预期表达式”错误: #pragma mark - #pragma mark picker methods - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return kPICKERCOLUMN; } - (NSInteger)pickerView:(UIPickerView

我在一个视图控件中有多个PickerView。它适用于2个选择器视图,但当我添加更多视图时,在以下代码中会出现7个“预期表达式”错误:

#pragma mark -
#pragma mark picker methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return kPICKERCOLUMN;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
        return [categoryTypes count];
    else    
        return [locationTypes count];
    else
        return [originatorTypes count];
    else
        return [destinationTypes count];
    else
        return [statusTypes count];

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == kCATEGORYTYPEPICKERTAG)       
        return [categoryTypes objectAtIndex:row];
    else
        return [locationTypes objectAtIndex:row];
    else
        return [originatorTypes objectAtIndex:row];
    else
        return [destinationTypes objectAtIndex:row];
    else
        return [statusTypes objectAtIndex:row];

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == kCATEGORYTYPEPICKERTAG) {
        NSString *categoryType  = [categoryTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [categoryTypeBtn setTitle:categoryType forState:UIControlStateNormal];  

    }else {

        NSString *locationType  = [locationTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [locationTypeBtn setTitle:locationType forState:UIControlStateNormal];

    }else {

        NSString *originatorType  = [originatorTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [originatorTypeBtn setTitle:originatorType forState:UIControlStateNormal];

    }else {

        NSString *destinationType  = [destinationTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [destinationTypeBtn setTitle:destinationType forState:UIControlStateNormal];

    }else {

        NSString *statusType  = [statusTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [statusTypeBtn setTitle:statusType forState:UIControlStateNormal];

    }

}
所有7个错误都显示在单独的
else
行中

希望我错过了一些简单的事情

非常感谢

更新

我现在有了所有的选择器视图。但是,唯一更改的信息是最后一个按钮,而不是每个按钮,具体取决于选择的内容

以下是完整的.m文件:

#define kPICKERCOLUMN 1
#define kCATEGORYTYPEPICKERTAG 20
#define kLOCATIONTYPEPICKERTAG 21
#define kORIGINATORTYPEPICKERTAG 22
#define kDESTINATIONTYPEPICKERTAG 23
#define kSTATUSTYPEPICKERTAG 24

@implementation HomeViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        categoryTypes = [[NSArray alloc] initWithObjects:@"Appetizers",@"Breakfast",@"Dessert",@"Drinks",
                         @"Main Dish/Entree", @"Salad", @"Side Dish", @"Soup", @"Snack", 
                         @"Baby Food", @"Pet Food",nil];  

        locationTypes = [[NSArray alloc] initWithObjects:@"African",@"American",@"Armenian",@"Barbecue",
                         @"Brazilian", @"British", @"Cajun", @"Central American", @"Chicken",
                         @"Chinese", @"Cuban",
                         @"Ethiopian", @"French", @"Greek", @"German", @"Hamburgers",
                         @"Homestyle Cooking", @"Indian", @"Irish", @"Italian", @"Jamaican",
                         @"Japanese", @"Korean", @"Mexican", @"Middle Eastern", @"Pakistani",
                         @"Pancakes /Waffles", @"Persian", @"Pizza", @"Polynesian", @"Russian",
                         @"Sandwiches", @"Seafood", @"Scandinavian", @"Spanish", @"Soul Food",
                         @"South American", @"Steak", @"Vegetarian", @"Tex-Mex", @"Thai",
                         @"Vietnamese",@"Wild Game",nil];

        originatorTypes = [[NSArray alloc] initWithObjects:@"African",@"American",@"Armenian",@"Barbecue",
                         @"Brazilian", @"British", @"Cajun", @"Central American", @"Chicken",
                         @"Chinese", @"Cuban",
                         @"Ethiopian", @"French", @"Greek", @"German", @"Hamburgers",
                         @"Homestyle Cooking", @"Indian", @"Irish", @"Italian", @"Jamaican",
                         @"Japanese", @"Korean", @"Mexican", @"Middle Eastern", @"Pakistani",
                         @"Pancakes /Waffles", @"Persian", @"Pizza", @"Polynesian", @"Russian",
                         @"Sandwiches", @"Seafood", @"Scandinavian", @"Spanish", @"Soul Food",
                         @"South American", @"Steak", @"Vegetarian", @"Tex-Mex", @"Thai",
                         @"Vietnamese",@"Wild Game",nil];


        destinationTypes = [[NSArray alloc] initWithObjects:@"African",@"American",@"Armenian",@"Barbecue",
                         @"Brazilian", @"British", @"Cajun", @"Central American", @"Chicken",
                         @"Chinese", @"Cuban",
                         @"Ethiopian", @"French", @"Greek", @"German", @"Hamburgers",
                         @"Homestyle Cooking", @"Indian", @"Irish", @"Italian", @"Jamaican",
                         @"Japanese", @"Korean", @"Mexican", @"Middle Eastern", @"Pakistani",
                         @"Pancakes /Waffles", @"Persian", @"Pizza", @"Polynesian", @"Russian",
                         @"Sandwiches", @"Seafood", @"Scandinavian", @"Spanish", @"Soul Food",
                         @"South American", @"Steak", @"Vegetarian", @"Tex-Mex", @"Thai",
                         @"Vietnamese",@"Wild Game",nil];


        statusTypes = [[NSArray alloc] initWithObjects:@"African",@"American",@"Armenian",@"Barbecue",
                         @"Brazilian", @"British", @"Cajun", @"Central American", @"Chicken",
                         @"Chinese", @"Cuban",
                         @"Ethiopian", @"French", @"Greek", @"German", @"Hamburgers",
                         @"Homestyle Cooking", @"Indian", @"Irish", @"Italian", @"Jamaican",
                         @"Japanese", @"Korean", @"Mexican", @"Middle Eastern", @"Pakistani",
                         @"Pancakes /Waffles", @"Persian", @"Pizza", @"Polynesian", @"Russian",
                         @"Sandwiches", @"Seafood", @"Scandinavian", @"Spanish", @"Soul Food",
                         @"South American", @"Steak", @"Vegetarian", @"Tex-Mex", @"Thai",
                         @"Vietnamese",@"Wild Game",nil];

    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    categoryTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,250,400,160)];
    categoryTypePicker.tag = kCATEGORYTYPEPICKERTAG;
    categoryTypePicker.showsSelectionIndicator = TRUE;
    categoryTypePicker.dataSource = self;
    categoryTypePicker.delegate = self;
    categoryTypePicker.hidden = YES;
    locationTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,250,400,160)];
    locationTypePicker.backgroundColor = [UIColor blueColor];
    locationTypePicker.tag = kLOCATIONTYPEPICKERTAG;
    locationTypePicker.showsSelectionIndicator = TRUE;
    locationTypePicker.hidden = YES;
    locationTypePicker.dataSource = self;
    locationTypePicker.delegate = self;
    originatorTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,250,400,160)];
    originatorTypePicker.backgroundColor = [UIColor blueColor];
    originatorTypePicker.tag = kORIGINATORTYPEPICKERTAG;
    originatorTypePicker.showsSelectionIndicator = TRUE;
    originatorTypePicker.hidden = YES;
    originatorTypePicker.dataSource = self;
    originatorTypePicker.delegate = self;
    destinationTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,250,400,160)];
    destinationTypePicker.backgroundColor = [UIColor blueColor];
    destinationTypePicker.tag = kDESTINATIONTYPEPICKERTAG;
    destinationTypePicker.showsSelectionIndicator = TRUE;
    destinationTypePicker.hidden = YES;
    destinationTypePicker.dataSource = self;
    destinationTypePicker.delegate = self;
    statusTypePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,250,400,160)];
    statusTypePicker.backgroundColor = [UIColor blueColor];
    statusTypePicker.tag = kSTATUSTYPEPICKERTAG;
    statusTypePicker.showsSelectionIndicator = TRUE;
    statusTypePicker.hidden = YES;
    statusTypePicker.dataSource = self;
    statusTypePicker.delegate = self;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eve
{

    if ( !locationTypePicker.hidden) {
        locationTypePicker.hidden = YES;
    }
    if ( !categoryTypePicker.hidden) {
        categoryTypePicker.hidden = YES;
    }
    if ( !originatorTypePicker.hidden) {
        originatorTypePicker.hidden = YES;
    }
    if ( !destinationTypePicker.hidden) {
        destinationTypePicker.hidden = YES;
    }
    if ( !statusTypePicker.hidden) {
        statusTypePicker.hidden = YES;
    }

}

#pragma mark -
#pragma mark picker methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return kPICKERCOLUMN;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    switch (pickerView.tag) {
        case kCATEGORYTYPEPICKERTAG:
            return [categoryTypes count];;
            break;
        case kLOCATIONTYPEPICKERTAG:
            return [locationTypes count];
            break;
        case kORIGINATORTYPEPICKERTAG:
            return [originatorTypes count];
            break;
        case kDESTINATIONTYPEPICKERTAG:
            return [destinationTypes count];
            break;
        case kSTATUSTYPEPICKERTAG:
            return [statusTypes count];
            break;
        default:
            break;
    }


}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (pickerView.tag) {
        case kCATEGORYTYPEPICKERTAG:
            return [categoryTypes objectAtIndex:row];
            break;
        case kLOCATIONTYPEPICKERTAG:
            return [locationTypes objectAtIndex:row];
            break;
        case kORIGINATORTYPEPICKERTAG:
            return [originatorTypes objectAtIndex:row];
            break;
        case kDESTINATIONTYPEPICKERTAG:
            return [destinationTypes objectAtIndex:row];
            break;
        case kSTATUSTYPEPICKERTAG:
            return [statusTypes objectAtIndex:row];
            break;
        default:
            break;
    }


}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == kCATEGORYTYPEPICKERTAG) {
        NSString *categoryType  = [categoryTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [categoryTypeBtn setTitle:categoryType forState:UIControlStateNormal];  

    }else if (pickerView.tag == kLOCATIONTYPEPICKERTAG) {

        NSString *locationType  = [locationTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [locationTypeBtn setTitle:locationType forState:UIControlStateNormal];

    }else if (pickerView.tag == kORIGINATORTYPEPICKERTAG) {

        NSString *originatorType  = [originatorTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [originatorTypeBtn setTitle:originatorType forState:UIControlStateNormal];

    }else if (pickerView.tag == kDESTINATIONTYPEPICKERTAG) {

        NSString *destinationType  = [destinationTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [destinationTypeBtn setTitle:destinationType forState:UIControlStateNormal];

    }else if (pickerView.tag == kSTATUSTYPEPICKERTAG) {

        NSString *statusType  = [statusTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
        [statusTypeBtn setTitle:statusType forState:UIControlStateNormal];

    }

}
-(IBAction) showLocationTypePicker{
    if ( categoryTypePicker.hidden) {
        categoryTypePicker.hidden = NO;
        [self.view addSubview:categoryTypePicker];  
    }
    else {
        categoryTypePicker.hidden = NO;
        [categoryTypePicker removeFromSuperview];
    }
    if ( originatorTypePicker.hidden) {
        originatorTypePicker.hidden = NO;
        [self.view addSubview:originatorTypePicker];
    }
    else {
        originatorTypePicker.hidden = NO;
        [originatorTypePicker removeFromSuperview];
    }
    if ( destinationTypePicker.hidden) {
        destinationTypePicker.hidden = NO;
        [self.view addSubview:destinationTypePicker];
    }
    else {
        destinationTypePicker.hidden = NO;
        [destinationTypePicker removeFromSuperview];
    }
    if ( statusTypePicker.hidden) {
        statusTypePicker.hidden = NO;
        [self.view addSubview:statusTypePicker];
    }
    else {
        statusTypePicker.hidden = NO;
        [statusTypePicker removeFromSuperview];
    }



}
-(IBAction) showCategoryTypePicker{ 
    if ( locationTypePicker.hidden) {
        locationTypePicker.hidden = NO;
        [self.view addSubview:locationTypePicker];
    }
    else {
        locationTypePicker.hidden = NO;
        [locationTypePicker removeFromSuperview];
    }
    if ( originatorTypePicker.hidden) {
        originatorTypePicker.hidden = NO;
        [self.view addSubview:originatorTypePicker];
    }
    else {
        originatorTypePicker.hidden = NO;
        [originatorTypePicker removeFromSuperview];
    }
    if ( destinationTypePicker.hidden) {
        destinationTypePicker.hidden = NO;
        [self.view addSubview:destinationTypePicker];
    }
    else {
        destinationTypePicker.hidden = NO;
        [destinationTypePicker removeFromSuperview];
    }
    if ( statusTypePicker.hidden) {
        statusTypePicker.hidden = NO;
        [self.view addSubview:statusTypePicker];
    }
    else {
        statusTypePicker.hidden = NO;
        [statusTypePicker removeFromSuperview];
    }
}

    -(IBAction) showOriginatorTypePicker{
        if ( locationTypePicker.hidden) {
            locationTypePicker.hidden = NO;
            [self.view addSubview:locationTypePicker];
        }
        else {
            locationTypePicker.hidden = NO;
            [locationTypePicker removeFromSuperview];
        }
        if ( categoryTypePicker.hidden) {
            categoryTypePicker.hidden = NO;
            [self.view addSubview:categoryTypePicker];
        }
        else {
            categoryTypePicker.hidden = NO;
            [categoryTypePicker removeFromSuperview];
        }
        if ( destinationTypePicker.hidden) {
            destinationTypePicker.hidden = NO;
            [self.view addSubview:destinationTypePicker];
        }
        else {
            destinationTypePicker.hidden = NO;
            [destinationTypePicker removeFromSuperview];
        }
        if ( statusTypePicker.hidden) {
            statusTypePicker.hidden = NO;
            [self.view addSubview:statusTypePicker];
        }
        else {
            statusTypePicker.hidden = NO;
            [statusTypePicker removeFromSuperview];
        }
    }

-(IBAction) showDestinationTypePicker{
    if ( locationTypePicker.hidden) {
        locationTypePicker.hidden = NO;
        [self.view addSubview:locationTypePicker];
    }
    else {
        locationTypePicker.hidden = NO;
        [locationTypePicker removeFromSuperview];
    }
    if ( originatorTypePicker.hidden) {
        originatorTypePicker.hidden = NO;
        [self.view addSubview:originatorTypePicker];
    }
    else {
        originatorTypePicker.hidden = NO;
        [originatorTypePicker removeFromSuperview];
    }
    if ( categoryTypePicker.hidden) {
        categoryTypePicker.hidden = NO;
        [self.view addSubview:categoryTypePicker];
    }
    else {
        categoryTypePicker.hidden = NO;
        [categoryTypePicker removeFromSuperview];
    }
    if ( statusTypePicker.hidden) {
        statusTypePicker.hidden = NO;
        [self.view addSubview:statusTypePicker];
    }
    else {
        statusTypePicker.hidden = NO;
        [statusTypePicker removeFromSuperview];
    }

}

-(IBAction) showStatusTypePicker{
    if ( locationTypePicker.hidden) {
        locationTypePicker.hidden = NO;
        [self.view addSubview:locationTypePicker];
    }
    else {
        locationTypePicker.hidden = NO;
        [locationTypePicker removeFromSuperview];
    }
    if ( originatorTypePicker.hidden) {
        originatorTypePicker.hidden = NO;
        [self.view addSubview:originatorTypePicker];
    }
    else {
        originatorTypePicker.hidden = NO;
        [originatorTypePicker removeFromSuperview];
    }
    if ( destinationTypePicker.hidden) {
        destinationTypePicker.hidden = NO;
        [self.view addSubview:destinationTypePicker];
    }
    else {
        destinationTypePicker.hidden = NO;
        [destinationTypePicker removeFromSuperview];
    }
    if ( categoryTypePicker.hidden) {
        categoryTypePicker.hidden = NO;
        [self.view addSubview:categoryTypePicker];
    }
    else {
        categoryTypePicker.hidden = NO;
        [categoryTypePicker removeFromSuperview];
    }






}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [locationTypes release];
    [categoryTypes release];
    [originatorTypes release];
    [destinationTypes release];
    [statusTypes release];
    [categoryTypePicker release];
    [locationTypePicker release];
    [originatorTypePicker release];
    [destinationTypePicker release];
    [statusTypePicker release];;
    [super dealloc];
}

语法问题是直截了当的:其他人需要条件,如:

if (pickerView.tag == kCATEGORYTYPEPICKERTAG)
    return [categoryTypes count];
else if (pickerView.tag == kSOMEOTHERTAG_THAT_MEANS_USE_LOCATION_TYPES)
    return [locationTypes count];
else if (// etc.
if
可以有一个无条件的
else
。这意味着else条件是if的补充,即

if (someCondition) {
    // some statements
} else {  // this implies: else if (!someCondition) 
    // some statements
}
但任何一个以上都会变得模棱两可。
switch
语句将是更好的选择

switch (pickerView.tag) {
    case kCATEGORYTYPEPICKERTAG:
        return categoryTypes count];
        break;
    case kSOMEOTHERTAG_THAT_MEANS_USE_LOCATION_TYPES:
        return [locationTypes count];
    // etc 
    default:
        break;
}

但我想知道,打开picker view的标记,逻辑是否合理。您可以这样做来区分许多选择器视图——这就是您的意思吗?也许条件分支应该在单个选取器视图中选取的对象(selectedRow)上进行?

如果(somthing){somthing}else{somthing}else{anything}else{of what?}你真的有很多具有不同标记的选取器视图吗,还是应该打开单个选择器视图的selectedRow?@danh我有多个具有不同标记的选择器视图。其思想是,它在发送时填充电子邮件,不同的选取器视图将填充电子邮件中的不同行。好的,您是否修复了其他问题?我不太明白。这可能是他自己的问题。也许从调试程序开始,看看它是否按照您期望的方式进行分支。是的,另一个问题现在已经解决了。我想我要开始一个新问题。谢谢你的帮助,真的很感激。非常感谢!!我现在面临的问题是,无论我选择了什么样的选择器视图,它都只会更改最终选择。