2012-02-22 62 views
9

iOS應用程序的對象庫中是否存在任何複選框控件?我看到的最接近的是開關控件,它可以採用布爾值。iOS應用程序的複選框控件

+0

UISegmentedControl可以幫助你 – 0xDE4E15B 2012-02-22 07:48:23

+1

使用的UIButton狀態爲選中/清除標記。放入圖片取消選中(正常狀態)並選中(選中狀態)。 – Hirak 2012-02-22 07:49:59

+0

沒有沒有默認的用戶界面控制複選框或單選按鈕,在這裏你必須使用2個不同的圖像進行檢查和未選中,並在邏輯上控制它們 – kulss 2012-02-22 07:49:35

回答

36

您可以使用一個UIButton的選中狀態,設置不同的圖像(或者也文本)的型動物(通過代碼或Interface Builder中):

[button setImage:[UIImage imageNamed:@"selected.png"] 
     forState:UIControlStateSelected]; 
[button setImage:[UIImage imageNamed:@"unselected.png"] 
     forState:UIControlStateNormal]; 

而且在觸摸的內心操作:

- (IBAction)buttonTouched:(id)sender 
{ 
    button.selected = !button.selected; 
    // add other logic 
} 
+0

對我來說非常有用。謝謝..... – Vaquita 2012-11-28 14:09:58

+0

這是一個開源庫,爲它提供實現。 https://github.com/t4ku/RadioButtonWithUIKit – 2013-03-04 09:20:55

+0

不起作用。你必須採取UIImageView。 – 2014-01-06 10:18:54

1

這是我用於屏幕上的兩個複選框的代碼。 (我把它們放在屏幕上的兩張圖片的底部,我用UIControlEventTouchDown調用checkBoxTapped方法,這個方法和我的主視圖控制器對話,我決定答案是否正確或不正確,然後主視圖控制器調用回該視圖並告訴它的空白文本框更改爲一個檢查:

enter image description here

或x

enter image description here

// Put the Checkboxes on the screen 
UIImage *checkBox = [UIImage imageNamed:@"Checkbox"]; 

self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom]; 
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal]; 
[self.checkBoxLeft setFrame:checkBoxFrameLeft]; 
[self.checkBoxLeft addTarget:self 
         action:@selector(checkBoxTapped:) 
      forControlEvents:UIControlEventTouchDown]; 

[self.checkBoxLeft setTitle:@"checkBoxLeft" forState:UIControlStateNormal]; 
self.checkBoxLeft.contentEdgeInsets = insets; 
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray 

self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom]; 
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal]; 
[self.checkBoxRight setFrame:checkBoxFrameRight]; 
[self.checkBoxRight addTarget:self 
         action:@selector(checkBoxTapped:) 
      forControlEvents:UIControlEventTouchDown]; 

[self.checkBoxRight setTitle:@"checkBoxRight" forState:UIControlStateNormal]; 
self.checkBoxRight.contentEdgeInsets = insets; 
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray 

- (void)checkBoxTapped:(UIButton *)buttonPressed { 
    [[self delegate] checkBoxTapped:buttonPressed.currentTitle]; 
} 

- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight { 

    if ([checkboxToHighlight isEqualToString:@"left"]){ 
     [self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal]; 
    } else { 
     [self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal]; 
    } 
} 
0

//定義按鈕

的屬性聲明未經檢查的圖片

- (void)viewDidLoad 
{ 
    [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal]; 
} 

檢測圖像

- (IBAction)buttonTapped:(id)sender 
{ 
    if (_checkboxButton.selected == YES) 
    { 
     [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateSelected]; 
     _checkboxButton.selected = NO; 
    } 
    else 
    { 
     [_checkboxButton setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal]; 
     _checkboxButton.selected = YES; 
    } 
}