2012-12-18 51 views
-3

在我的應用程序中,在UIButton Click上,我需要動態創建多個UIButton併爲其執行方法。如何刪除多個UIButton或特定的UIButton動態創建?

如何從創建的UIButtons中刪除特定的UIButton? 按退格鍵按下或刪除按鍵?或者有沒有其他方法可以刪除UIButton?

我的源代碼在.h文件中

NSMutableArray *selectedBtnarr; 

在.m文件

-(void)Check 
{ 
    CGPoint origin = note.frame.origin; 
    NSString* head = [note.text substringToIndex:note.selectedRange.location]; 
    CGSize initialSize = [head sizeWithFont:note.font constrainedToSize:note.contentSize]; 
    NSUInteger startOfLine = [head length]; 

    NSString* tail = [head substringFromIndex:startOfLine]; 
    CGSize lineSize = [tail sizeWithFont:note.font forWidth:note.contentSize.width lineBreakMode:UILineBreakModeWordWrap]; 
    CGPoint cursor = origin; 
    cursor.x += lineSize.width + 15; 
    cursor.y += initialSize.height - lineSize.height - 130; 


    checkbox = [[UIButton alloc] initWithFrame:CGRectMake(cursor.x,cursor.y,15,15)]; 
    [checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal]; 
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateSelected]; 
    [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateHighlighted]; 
    checkbox.adjustsImageWhenHighlighted=YES; 
    [checkbox setTag:checkButtonCount]; 
    [checkbox addTarget:self action:@selector(ChkUnChk:) forControlEvents:UIControlEventTouchUpInside]; 
    [note addSubview:checkbox]; 

    NSString *xAxis=[NSString stringWithFormat:@"%f" ,checkbox.frame.origin.x]; 
    NSString *yAxis=[NSString stringWithFormat:@"%f" ,checkbox.frame.origin.y]; 
    [chkBoxX addObject:xAxis]; 
    [chkBoxY addObject:yAxis]; 
    NSString *tag=[NSString stringWithFormat:@"%d" ,checkButtonCount]; 
    [chkTag addObject:tag]; 
    checkButtonCount=checkButtonCount+1; 

     } 

-(void)ChkUnChk:(id)sender 
    { 
    UIButton *btn=(UIButton *)sender; 
    NSString *Str=[NSString stringWithFormat:@"%d",btn.tag]; 
    BOOL flag= [selectedBtnarr containsObject:Str]; 

    if (flag==YES) 
    { 
     [btn setBackgroundImage:[UIImage imageNamed:@"unchk.png"] forState:UIControlStateNormal]; 
     [selectedBtnarr removeObject:Str]; 
    } 
    else 
    { 
     [selectedBtnarr addObject:Str]; 
     [btn setBackgroundImage:[UIImage imageNamed:@"chk.png"] forState:UIControlStateNormal]; 
    } 
    } 

但我DONO如何刪除這個創造

...

到刪除我的代碼是

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    const char * _char = [text cStringUsingEncoding:NSUTF8StringEncoding]; 
    int isBackSpace = strcmp(_char, "\b"); 

    if (isBackSpace == -8) 
    { 
     NSLog(@"backspace Detected"); 

     CGPoint origin = note.frame.origin; 
     NSString* head = [note.text substringToIndex:note.selectedRange.location]; 
     CGSize initialSize = [head sizeWithFont:note.font constrainedToSize:note.contentSize]; 
     NSUInteger startOfLine = [head length]; 

     NSString* tail = [head substringFromIndex:startOfLine]; 
     CGSize lineSize = [tail sizeWithFont:note.font forWidth:note.contentSize.width lineBreakMode:UILineBreakModeWordWrap]; 
     CGPoint cursor = origin; 
     cursor.x += lineSize.width + 15; 
     cursor.y += initialSize.height - lineSize.height - 130; 

     NSLog(@"Cursor Place X : %f Y: %f",cursor.x,cursor.y); 

     int tagcoount=[chkTag count]; 

     for(int a=0;a<tagcoount;a++) 
     { 

      NSString *tag=[NSString stringWithFormat:@"%@" ,[chkTag objectAtIndex:a]]; 

      int chkcnt=[tag intValue]; 

      if(cursor.x==checkbox.frame.origin.x && cursor.y==checkbox.frame.origin.y && a==checkbox.tag) 
       { 
        [[checkbox viewWithTag:chkcnt] removeFromSuperview]; 
        [chkTag removeObjectAtIndex:chkcnt]; 

       } 
     } 
} 

但它刪除只有最後的UIButton,太當光標在當前位置僅..給我正確的想法

+0

你有沒有發現我的答案? –

+0

stil am在那 – Rajendran

+0

你有沒有檢查我的答案,並嘗試下載我的代碼? –

回答

2

使用NSMutableArray存儲UIButton對象。使用其setTag:方法爲每個UIButton分配一個標籤號碼以識別各個按鈕並相應地將其刪除。

+0

非常感謝,我已經更新了代碼。請檢查它,並說如何寫刪除 – Rajendran

+0

您是否嘗試過使用NSLog來查看哪些chkcnt值正在發送到[chkTag removeObjectAtIndex:chkcnt]? –

1

根據您的要求:請從我的Mac project的想法(如IOS我不是),但你會覺得這個方便。

@interface AppDelegate : NSObject <NSApplicationDelegate> 

@property (assign) IBOutlet NSWindow *window; 

@property(strong)NSMutableArray *buttons; 
- (IBAction)add:(id)sender; 
- (IBAction)remove:(id)sender; 
@property (strong) IBOutlet NSPopUpButton *popUp; 
@property (strong) IBOutlet NSView *buttonView; 

@end 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{ 
    _buttons=[NSMutableArray new]; 

} 

-(void)clearButtonSubview{ 
    for (NSInteger i=0; i<[[_buttonView subviews]count]; i++) { 
     NSView *view=[[_buttonView subviews]objectAtIndex:i]; 
     [view removeFromSuperview]; 
    } 
} 

-(void)drawButtons{ 

    //when u add, remove prev drawn and redraw all. 
    [self clearButtonSubview]; 

    for (NSInteger i=0; i<[_buttons count]; i++) { 
     NSButton *button=[_buttons objectAtIndex:i]; 
     [button setFrame:NSMakeRect(i*55, 10, 50, 20)]; 
     [_buttonView addSubview:button]; 

     //add buttons in popup 
     [_popUp addItemWithTitle:[NSString stringWithFormat:@"%ld",button.tag]]; 
    } 

} 

- (IBAction)add:(id)sender { 

    [_popUp removeAllItems]; 

    NSButton *bttn=[NSButton new]; 
    //get last button tag 
    NSInteger lastButtonTag=[[_buttons lastObject] tag]; 

    [bttn setTag:lastButtonTag+1]; 
    [bttn setTitle:[NSString stringWithFormat:@"%ld",[bttn tag]]]; 

    [_buttons addObject:bttn]; 

    [self drawButtons]; 
} 

- (IBAction)remove:(id)sender { 
    NSInteger indexOfButtonToRemove; 
    NSString *selectedButton=[_popUp titleOfSelectedItem]; 

    for (NSInteger index=0;index<[_buttons count];index++) { 
     NSButton *button=[_buttons objectAtIndex:index]; 

     if ([[button title]isEqualToString:selectedButton]) { 
      indexOfButtonToRemove=index; 
      break; 
     } 
    } 

    [_buttons removeObjectAtIndex:indexOfButtonToRemove]; 
    [_popUp removeItemAtIndex:indexOfButtonToRemove]; 

    [self clearButtonSubview]; 
    [self drawButtons]; 
} 

@end 
+0

非常感謝你...我已更新我的問題n添加了我的代碼..請看看並說我如何刪除UIButton – Rajendran

+0

檢查我的更新: –

+0

謝謝。在我上面的代碼中,我需要做這個改變 – Rajendran