Remove the old button when double tap
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:@"restaurant.png"] forState:UIControlStat开发者_如何学GoeNormal];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"restaurant1.png"] forState:UIControlStateNormal];
if([scaleStr isEqualToString:@"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:@"0.209607"]) {
[btn1 removeFromSuperView];
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
i have set the button when doubletap the background image in UIScrollview.If i double tap second time , the first button also show.I need to remove the button which i created first .
Also i remove the btn1 in next condition.
Kindly help me regarding on this.
A little bit of context might help here, but my guess is that you're really just creating a new button each time and not holding a reference to the old one. So when you call removeFromSuperView on the second tap, you're removing a button that hasn't even been added to the view, not the one you added on your last pass. Try making your buttons instance variables instead of local. In extremely loose pseudo-code:
@interface MyClass : UIView {
UIButton *btn1;
UIButton *btn2;
}
@implementation MyClass {
-(MyClass *) init {
self = [super init];
[self setupButtons];
return self;
}
-(void) setupButtons {
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:@"restaurant.png"] forState:UIControlStateNormal];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:@"restaurant1.png"] forState:UIControlStateNormal];
}
-(void) myFunction {
[btn1 removeFromSuperView];
[btn2 removeFromSuperView];
if([scaleStr isEqualToString:@"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:@"0.209607"]) {
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
}
}
精彩评论