2010-08-04 55 views
0
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btn1 setImage:[UIImage imageNamed:@"restaurant.png"] forState:UIControlStateNormal]; 
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];  

} 

我已經doubletap在UIScrollview.If背景圖像時設置的按鈕i抽頭第二時間加倍,第一按鈕也show.I要刪除哪些我創建第一按鈕。刪除舊的按鈕時雙抽頭

另外我在下一個條件中刪除btn1。

請在此幫助我。

回答

0

有一點上下文可能對此有所幫助,但我的猜測是,您實際上只是每次創建一個新按鈕,而沒有參照舊的按鈕。因此,當您在第二次點擊時調用removeFromSuperView時,您將刪除尚未添加到視圖中的按鈕,而不是您添加到最後一個通道中的按鈕。嘗試使您的按鈕實例變量而不是本地。非常鬆散的僞代碼:


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