1

在我的应用程序中,当按下按钮 n 自定义imagePicker出现时,它包含我的图像NSDocumentDirectory,我想要的是当我选择一个图像时,它会:隐藏、删除、设置为选择,或者任何认为它被选中的东西。

我尝试过为它分配一个整数,所以下次我展示它时,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];

}
4

1 回答 1

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];
            }
        }
    }      
}
于 2012-07-19T09:13:23.157 回答