当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - UIButton created in part by NSArray being drawn twice

问题描述:

I'm trying to create a search function in my app where the results pop up as buttons below based on keywords from the menus that I have predefined. The menus and such are retrieved through a separate class. The problem I'm having is that if you type in two keywords from the menu, the buttons overlay and they won't all disappear when I call

[button removeFromSuperView];

Hopefully you can see what's going on based on this code, the search is the UITextField. I've tried making sure they won't get readded by using the containsObject method, but it doesn't seem to be working. Any help would be much appreciated. The NSArray inputs is to check each word individually incase multiple places have it.

NSString *userInput = search.text;

NSMutableArray *choices = [[NSMutableArray alloc]initWithCapacity:3];

NSArray *inputs = [userInput componentsSeparatedByString:@" "];

for (int i = 0; i < inputs.count; i++) {

if (!([rootBeer.menu rangeOfString:inputs[i] options:NSCaseInsensitiveSearch].location == NSNotFound)){

if (!([choices containsObject:rootBeer])) {

[choices addObject:rootBeer];

}

} if (!([curry.menu rangeOfString:inputs[i] options:NSCaseInsensitiveSearch].location == NSNotFound)){

if (!([choices containsObject:curry])) {

[choices addObject:curry];

}

} if (!([hotDog.menu rangeOfString:inputs[i] options:NSCaseInsensitiveSearch].location == NSNotFound)){

if (!([choices containsObject:hotDog])) {

[choices addObject:hotDog];

}

} [self showOptions:choices];

-(void)showOptions:(NSArray *)array{

CGRect screenSize = [[UIScreen mainScreen]bounds];

CGFloat screenWidth = screenSize.size.width;

NSUInteger count = array.count;

for (int i=0; i<count; i++) {

RestaurantInfo *class = array[i];

if (i == 0) {

button1 = [UIButton buttonWithType:UIButtonTypeCustom];

[button1 addTarget:self action:@selector(restaurantPressed:) forControlEvents:UIControlEventTouchUpInside];

[button1 setTitle:class.name forState:UIControlStateNormal];

[button1 setFrame:CGRectMake(0, 40 + ((i + 1) * 40), screenWidth, 40)];

[UIView animateWithDuration:time - 0.05 animations:^{

[button1 setFrame:CGRectMake(0, 0 + (i * 40), screenWidth, 40)];

}];

[button1 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];

[button1 setTag:class.tag];

[self.view addSubview:button1];

} else if(i == 1) {

RestaurantInfo *class = array[i];

button2 = [UIButton buttonWithType:UIButtonTypeCustom];

[button2 addTarget:self action:@selector(restaurantPressed:) forControlEvents:UIControlEventTouchUpInside];

[button2 setTitle:class.name forState:UIControlStateNormal];

[button2 setFrame:CGRectMake(0, 40 + ((i + 1) * 40), screenWidth, 40)];

[UIView animateWithDuration:time - 0.05 animations:^{

[button2 setFrame:CGRectMake(0, 0 + (i * 40), screenWidth, 40)];

}];

[button2 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];

[button2 setTag:class.tag];

[self.view addSubview:button2];

} else if(i == 2) {

RestaurantInfo *class = array[i];

button3 = [UIButton buttonWithType:UIButtonTypeCustom];

[button3 addTarget:self action:@selector(restaurantPressed:) forControlEvents:UIControlEventTouchUpInside];

[button3 setTitle:class.name forState:UIControlStateNormal];

[button3 setFrame:CGRectMake(0, 40 + ((i + 1) * 40), screenWidth, 40)];

[UIView animateWithDuration:time - 0.05 animations:^{

[button3 setFrame:CGRectMake(0, 0 + (i * 40), screenWidth, 40)];

}];

[button3 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];

[button3 setTag:class.tag];

[self.view addSubview:button3];

}

}

}

Additionally, is there a better way to create these buttons based on how many items I have in the array? Right now I have them being referenced in the .h file. Thanks!

分享给朋友:
您可能感兴趣的文章:
随机阅读: