将UIButton设置为已选中

时间:2012-07-19 06:44:35

标签: iphone ios xcode

在我的应用程序中,当按下某个按钮时,会出现自定义imagePicker,它包含NSDocumentDirectory中的图像,我想要的是当我选择图像时它会:HIDE,REMOVE,SET作为SELECTED,或任何将其视为选中的内容。

我已经做了什么,是为它分配一个整数,所以下次我提出imagePicker它会做上面的任何一个,仍然,没有运气。

您能否建议我如何解决我的问题或任何建议。谢谢。

这是我的代码:

- (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];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 


        [view addSubview:button];

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

    }

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

    [super viewDidLoad];
}

这是我尝试选择它的地方:

- (void) viewDidAppear:(BOOL)animated{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger selectedImage = [prefs integerForKey:@"selected"];
    if ( selectedImage ){
        [myButton removeFromSuperview];
        [myButton setBackgroundImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateSelected];
        [myButton setSelected:YES];
        [myButton setHidden:YES];
        [myButton setHighlighted:YES];
        myButton.selected = YES;
    }      
}

点击时:

- (IBAction)buttonClicked:(id)sender {
    self.myButton = (UIButton *)sender;
    self.selectedImage = [_images objectAtIndex:myButton.tag];

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


    [[NSUserDefaults standardUserDefaults] synchronize];

    [self dismissModalViewControllerAnimated:NO];
}

致电imagePicker

- (IBAction)chooseCustomImageTapped:(id)sender {
    _imagePicker1 = [[CustomImagePicker alloc] init];
    _imagePicker1.title = @"Choose Custom Image";
   for(int i = 0; i <= 30; i++) 
    { 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];

        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; 
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
            [_imagePicker1 addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 

        } 
    } 

     [self presentModalViewController:_imagePicker1 animated:NO];

}

1 个答案:

答案 0 :(得分:0)

尝试执行此操作以将按钮更改为选中状态:

- (void)turnButtonToSelected
{
    UIImage *bgImage = [UIImage imageNamed:@"Checkmark.png"];
    [self.button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [self.button setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [self.button setBackgroundImage:bgImage forState:UIControlStateDisabled];
}

但我必须说我不明白这个按钮是什么,你把它放在哪里? 这是网格上的按钮之一吗?

将它放在viewDidAppear而不是代码

- (void) viewDidAppear:(BOOL)animated{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger selectedImage = [prefs integerForKey:@"selected"];
    if ( selectedImage ){
        for (UIView *subview in self.view.subviews) {
            if ([subview isKindOfClass:[UIButton class]] && subview.tag == selectedImage) {
                [self turnButtonToSelected];
            }
        }
    }      
}