2012-12-17 122 views
1

我在UIViewController中有UITextView。在UITextView中,需要爲筆記插入多個複選框。如何以編程方式創建多個CheckBox

如何創建多個複選框?

I have created Multiple CheckBoxes for `UIButton` Click, But When I Select or DeSelect operation, all ChecKBoxes Value changes. 

如何動態創建多個CheckBox併爲這些CheckBox創建方法?

可能嗎?

這裏是我的代碼:

-(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 addTarget:self action:@selector(ChkUnChk) forControlEvents:UIControlEventTouchUpInside]; 
    [note addSubview:checkbox]; 
} 

-(void)ChkUnChk 
{ 
    if(checkUnCheck==NO) 
    { 
     [checkbox setBackgroundImage:[UIImage imageNamed:@"chk.png"]forState:UIControlStateNormal]; 
     checkUnCheck=YES; 
    } 
    else if(checkUnCheck==YES) 
    { 
     [checkbox setBackgroundImage:[UIImage imageNamed:@"unchk.png"]forState:UIControlStateNormal]; 
     checkUnCheck=NO; 
    } 
} 

-(void)checkboxSelected:(id)sender 
{ 
    checkBoxSelected = !checkBoxSelected; 
    [checkbox setSelected:checkBoxSelected]; 
} 

這裏要注意的 - >UITextView,複選框 - >UIButton

回答

6

乘坐NSMutableArray ..

在.h文件中

NSMutableArray *selectedBtnarr; 

在.m文件

- (void)viewDidLoad 
    { 
     selectedBtnarr=[NSMutableArray alloc]init]; 
    } 

然後,你必須設置的UIButton的標記屬性。每個按鈕都有不同的標籤。

-(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]; 
    } 
} 
+0

太感謝你了。它的工作很好。我怎樣才能刪除這些複選框使用退格鍵或刪除鍵 – Rajendran

1

嘗試設置按鈕將與任何整數創建的標籤(使用循環)。然後用-(void)ChkUnChk:(id)sender{}調用選擇器。因此,該方法只會被標記爲按鈕。

0

寫下2種方法。由於您已經爲不同的狀態分配了按鈕的不同方法,因此您只需要更改這些狀態​​並按照按鈕的狀態執行代碼。

-(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 addTarget:self action:@selector(ChkUnChk) forControlEvents:UIControlEventTouchUpInside]; 
    [note addSubview:checkbox]; 
} 

-(void)ChkUnChk:(id)sender 
{ 
    UIButton *btn = sender; 
    if(btn.selected==NO) 
    { 
     [btn setSelected:YES]; 
    } 
    else if(btn.selected==YES) 
    { 
     [btn setSelected:NO]; 
    } 
} 

讓我知道你是否卡在某個地方。

謝謝。

+0

感謝您的迴應..我有這個錯誤,當我輸入代碼裏面chkUnChk方法屬性'選定'沒有找到對象的類型'__strong id' – Rajendran

+0

我已經更新代碼現在....現在它會工作 – Hemang

+0

非常感謝你...現在如何刪除這些複選框,如果我創建更多...通過退格鍵或刪除鍵 – Rajendran

相關問題