我有一个自定义的 ImagePicker。AUIScrollView
带有我的图像,NSDocumentDirectory
然后我将按钮添加为我的图像框架,就像缩略图视图一样。我添加的是当它被选中然后关闭时,下次视图加载/出现/显示时,它将已经有一个图像复选标记。没关系。但我需要的是,当我点击一个图像时,它已经被选中或图像“复选标记”将显示,然后当我点击同一个图像时,复选标记将消失。
这是我拥有的代码的解释:
// UISCrollView
- (void)viewDidLoad {
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(@"%i",i);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected];
}
}
// 当按钮被点击时
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected1"];
[self dismissModalViewControllerAnimated:NO];
}
// 选择按钮时的方法
- (void)turnButtonToSelected
{
UIImage *bgImage = [UIImage imageNamed:@"Checkmark.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}