小智 32
这也让我很生气,我找到了一个适合我的不同解决方案,避免了使用图像.
您可能希望在viewDidLoad函数中调用togglePaidStatus函数,以便最初将其设置为空字符串.
显然,如果标签未启用,您可以添加额外的检查以防止用户切换复选框,但下面未显示.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if (CGRectContainsPoint([fullyPaid frame], [touch locationInView:self.view]))
{
[self togglePaidStatus];
}
}
-(void) togglePaidStatus
{
NSString *untickedBoxStr = [[NSString alloc] initWithString:@"\u2610"];
NSString *tickedBoxStr = [[NSString alloc] initWithString:@"\u2611"];
if ([fullyPaid.text isEqualToString:tickedBoxStr])
{
fullyPaid.text = untickedBoxStr;
}
else
{
fullyPaid.text = tickedBoxStr;
}
[tickedBoxStr release];
[untickedBoxStr release];
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*lje 25
通常,您可以将UISwitch用于类似复选框的功能.
您可以通过使用带有两个图像(已选中/未选中)的图像控件并在触摸控件时切换图像来自行滚动/
Bre*_*don 13
如果您正在显示一组选项,并且用户可以选择其中一个选项,请使用带有复选标记附件的表格视图以及所选行上的不同文本颜色.
如果您有一个选项,最好的办法是使用开关.如果您不能或不想,请使用按钮,将普通图像设置为空框,将所选图像设置为复选框.您必须自己制作这两张图片,或者找到可供他们使用的库存图片.
延伸到Adrean的想法,我使用一种非常简单的方法实现了这一目标.
我的想法是根据状态更改按钮(比如说checkBtn)文本,然后更改按钮的状态IBAction.
以下是我这样做的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[checkBtn setTitle:@"\u2610" forState:UIControlStateNormal]; // uncheck the button in normal state
[checkBtn setTitle:@"\u2611" forState:UIControlStateSelected]; // check the button in selected state
}
- (IBAction)checkButtonTapped:(UIButton*)sender {
sender.selected = !sender.selected; // toggle button's selected state
if (sender.state == UIControlStateSelected) {
// do something when button is checked
} else {
// do something when button is unchecked
}
}
Run Code Online (Sandbox Code Playgroud)
我想以编程方式执行此操作,并且还解决了命中区域实际上太小的问题.这可以从各种来源改编,包括Mike和Mike的评论员Agha.
在你的标题中
@interface YourViewController : UIViewController {
BOOL checkboxSelected;
UIButton *checkboxButton;
}
@property BOOL checkboxSelected;;
@property (nonatomic, retain) UIButton *checkboxButton;
-(void)toggleButton:(id)sender;
Run Code Online (Sandbox Code Playgroud)
并在您的实施中
// put this in your viewDidLoad method. if you put it somewhere else, you'll probably have to change the self.view to something else
// create the checkbox. the width and height are larger than actual image, because we are creating the hit area which also covers the label
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(100, 60,120, 44)];
[checkBox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
// uncomment below to see the hit area
// [checkBox setBackgroundColor:[UIColor redColor]];
[checkBox addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
// make the button's image flush left, and then push the image 20px left
[checkBox setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[checkBox setImageEdgeInsets:UIEdgeInsetsMake(0.0, 20.0, 0.0, 0.0)];
[self.view addSubview:checkBox];
// add checkbox text text
UILabel *checkBoxLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 74,200, 16)];
[checkBoxLabel setFont:[UIFont boldSystemFontOfSize:14]];
[checkBoxLabel setTextColor:[UIColor whiteColor]];
[checkBoxLabel setBackgroundColor:[UIColor clearColor]];
[checkBoxLabel setText:@"Checkbox"];
[self.view addSubview:checkBox];
// release the buttons
[checkBox release];
[checkBoxLabel release];
Run Code Online (Sandbox Code Playgroud)
并且也使用这种方法:
- (void)toggleButton: (id) sender
{
checkboxSelected = !checkboxSelected;
UIButton* check = (UIButton*) sender;
if (checkboxSelected == NO)
[check setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
else
[check setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
这是我的iphone复选框版本.
它是单一类,扩展了UIButton.这很简单所以我会把它贴在这里.
CheckBoxButton.h文件内容
#import <UIKit/UIKit.h>
@interface CheckBoxButton : UIButton
@property(nonatomic,assign)IBInspectable BOOL isChecked;
@end
Run Code Online (Sandbox Code Playgroud)
CheckBoxButton.m文件内容
#import "CheckBoxButton.h"
@interface CheckBoxButton()
@property(nonatomic,strong)IBInspectable UIImage* checkedStateImage;
@property(nonatomic,strong)IBInspectable UIImage* uncheckedStateImage;
@end
@implementation CheckBoxButton
-(id)init
{
self = [super init];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self)
{
[self addTarget:self action:@selector(switchState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)setIsChecked:(BOOL)isChecked
{
_isChecked = isChecked;
if(isChecked)
{
[self setImage:self.checkedStateImage forState:UIControlStateNormal];
}
else
{
[self setImage:self.uncheckedStateImage forState:UIControlStateNormal];
}
}
-(void)switchState
{
self.isChecked = !self.isChecked;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
@end
Run Code Online (Sandbox Code Playgroud)
您可以在visual studio的属性检查器中为已选中/未选中以及isChecked属性设置图像.

要在storyboard或xib中添加CheckBoxButton,只需添加UIButton并在下一个图像上设置自定义类.

每当isChecked状态改变时,Button都会发送UIControlEventValueChanged事件.
| 归档时间: |
|
| 查看次数: |
66827 次 |
| 最近记录: |