gpt4 book ai didi

iphone - 以编程方式更改按钮状态

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:50 26 4
gpt4 key购买 nike

我的按钮框架图像真的有问题,我想要的是:1.选中后会打勾2.当 View 被关闭然后返回到上一个屏幕时,它仍然会选中图像中的支票3. 当我点击所选图像或其他图像时,它会删除复选标记。

我能够做到,但我在设置图像时遇到问题:

我想要的是:

enter image description here

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];

但这是我需要的功能:

enter image description here

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];

下面是代码(如果我做的有问题请告诉我,谢谢):

- (void)viewDidLoad {
UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// 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];
}

[view addSubview:myButton];

if (column == 6) {
column = 0;
row++;
} else {
column++;
}

}

[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}

- (void)turnButtonToSelected
{ UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];

if (myButton.selected) {

[sender setSelected:NO];

}else {

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:myButton.tag forKey:@"slot1"];

[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected1"];

[[NSUserDefaults standardUserDefaults] synchronize];


[self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
[self dismissModalViewControllerAnimated:NO];

}

或者有没有一种方法可以在点击时禁用按钮时强制启用按钮?

最佳答案

我刚刚尝试了一种不同的方法。

对我来说它工作得很好并且达到了你的效果。

如果您需要进一步的解释或整个 Xcode 项目,请告诉我。

- (void)viewDidLoad
{
[super viewDidLoad];

//button 1
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(30, 50, 100, 80);
UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

//button 2
UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton2.frame = CGRectMake(180, 50, 100, 80);
UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
[imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
[imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

[self.view addSubview:imageButton];
[self.view addSubview:imageButton2];
}

- (void)buttonClicked:(UIButton*)button
{
//NSLog(@"Button clicked! %@", button);

UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
UIImage *empty = nil;

//NSLog(@"Imageview: %d", button.imageView.tag);
if (button.imageView.tag == 0)
{
[button setImage:checkmarkImage forState:UIControlStateNormal];
button.imageView.tag = 1;
}
else
{
[button setImage:empty forState:UIControlStateNormal];
button.imageView.tag = 0;
}
}

最好的,克里斯

更新:我已经更新了代码。这是整个 ViewController。注意:我会使用 coreData 来存储当前索引来解决这个问题。以下代码不是解决此问题的最佳方法,而是基于您的初始代码!

//
// SOViewController.m
// SOImageOverlay
//
// Created by Chris on 24.07.12.
//

#import "SOViewController.h"

@interface SOViewController ()

@end


#define CHECKMARK_IMAGE @"check.png"

@implementation SOViewController

@synthesize buttons = _buttons;

#pragma mark - Custom getter
- (NSMutableArray*)buttons
{
if (_buttons == nil)
_buttons = [[NSMutableArray alloc] init];

return _buttons;
}


#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];

// UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
// UIButton *myButton =
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// 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 < 2; ++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);*/

UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
if (i == 0)
{
UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
imageButton.frame = CGRectMake(30, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
else if (i == 1)
{
UIImage *btnImage = [UIImage imageNamed:@"DSC_5355.JPG"];
imageButton.frame = CGRectMake(180, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}

[imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];

if ( selectedImageSlot1 == i){
[self turnButtonToSelected:imageButton];
}

[self.view addSubview:imageButton];

//adding current button into buttons array
[self.buttons addObject:imageButton];

if (column == 6) {
column = 0;
row++;
} else {
column++;
}

}

//[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];


// //button 1
// UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton.frame = CGRectMake(30, 50, 100, 80);
// UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
// [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
// [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// //button 2
// UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton2.frame = CGRectMake(180, 50, 100, 80);
// UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
// [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
// [imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// [self.view addSubview:imageButton];
// [self.view addSubview:imageButton2];
}

//- (void)buttonClicked:(UIButton*)button
//{
// //NSLog(@"Button clicked! %@", button);
//
// UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
// UIImage *empty = nil;
//
// //NSLog(@"Imageview: %d", button.imageView.tag);
// if (button.imageView.tag == 0)
// {
// [button setImage:checkmarkImage forState:UIControlStateNormal];
// button.imageView.tag = 1;
// }
// else
// {
// [button setImage:empty forState:UIControlStateNormal];
// button.imageView.tag = 0;
// }
//}



// code from stackoverflow
// ------------------------------------------------------------
- (void)turnButtonToSelected:(UIButton*)button
{
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];

//UIButton *myButton = button; //just to use the copied code
button.selected = YES;
[button setImage:bgImage forState:UIControlStateSelected];


// [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}

- (IBAction)buttonClicked:(id)sender
{
UIButton *myButton = (UIButton *)sender;
//I have removed this line... due to the fact that I don't want to
//implement your arrays.
//self.selectedImage = [_images objectAtIndex:myButton.tag];

//deselecting all
for (UIButton *button in self.buttons)
{
button.selected = NO;
//[button setImage:nil forState:UIControlStateNormal];
}

if (myButton.selected) {

[sender setSelected:NO];

}else {

UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}


// NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// [prefs setInteger:myButton.tag forKey:@"slot1"];


//getting index of button in array
NSInteger index = 0;
NSInteger foundIndex = -1;
for (UIButton *button in self.buttons)
{
if (button == myButton)
{
foundIndex = index;
}
index++;
}

if (foundIndex >= 0)
{
[[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:@"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

[self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
// [self dismissModalViewControllerAnimated:NO];

}
// ------------------------------------------------------------


- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



- (IBAction)closeButtonClicked:(UIBarButtonItem *)sender
{
[self dismissModalViewControllerAnimated:YES];
}
@end

关于iphone - 以编程方式更改按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11431255/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com