按钮以编程方式更改状态

时间:2012-07-11 11:02:51

标签: iphone ios xcode

我的按钮框架图像真的有问题, 我想要的是: 1.选中它时会有一个复选标记 2.当视图被关闭然后返回到上一个屏幕时,它仍将检查所选图像 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];

}

或者有没有办法在点击时禁用强制启用按钮?

3 个答案:

答案 0 :(得分:4)

我刚尝试了另一种方法。

对我而言,它的效果很好,可以达到你的效果。

如果您需要进一步解释或整个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

答案 1 :(得分:3)

尝试这样做。

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


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

在代码的后半部分,您将按钮的状态设置为“选中”,但在上面的代码中,您没有。相反,您已禁用该按钮。试试我提供的上述代码。感谢。

答案 2 :(得分:-1)

当用户点击按钮

时,只需将chekedImage添加为按钮顶部的子视图

UIImage *button = [UIImage imageNamed:@"cat.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//add target button clicked 


-(void)buttonClicked : (id)sender{
    if(toogleSwitch){
       [button addSubview:checkedimageView]; // Just the checked image icon
    }
    else{
       [imageview removeFromSuperview];
    }
}

您尝试做的只会在您拥有时才会起作用 所有图像的两种类型的资源。 1)图像本身 2)带有复选框的图像作为透明层。