gpt4 book ai didi

iOS 多个选择器 View "Parse issue: Expected Expression"

转载 作者:行者123 更新时间:2023-11-29 02:52:26 26 4
gpt4 key购买 nike

我在 1 个 View 控件中有多个 pickerviews。它适用于 2 个选择器 View ,但当我添加更多时,它会在以下代码中出现 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 行中。

希望我错过的是一些简单的东西!!

非常感谢

更新

我现在可以使用所有选择器 View 。然而,唯一改变的唯一信息是最终按钮,而不是每个按钮,具体取决于所选择的内容。

这是完整的 .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;
}

但我想知道逻辑是否有意义,打开选择器 View 的标签。您会这​​样做以区分许多选择器 View ——您是这个意思吗?也许应该在单个选择器 View 中对已选择的内容 (selectedRow) 进行条件分支?

关于iOS 多个选择器 View "Parse issue: Expected Expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24345374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com