0

所以我有一个带有按钮的UIScrollviewwithUIImageView设置,我希望能够在单击图像alertView时弹出一个如果选择 YES 则该图像将在NSDocumentDirectory. 我设法使 alertView 出现,但图像没有删除,因为我认为发送错误sender或 button.tag。这是我的代码:

//我的滚动视图

UIScrollView *scrollView1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,134.0f)];
[self.view addSubview:scrollView1];

int row = 0;
int column = 0;
for(int i = 0; i < _thumbs1.count; ++i) {

    UIImage *thumb = [_thumbs1 objectAtIndex:i];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(column*60+10, row*60+10, 60, 60);
    [button setImage:thumb forState:UIControlStateNormal];
    [button addTarget:self 
               action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside];
    button.tag = i; 

    [scrollView1 addSubview:button];

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

// 按钮

- (IBAction)buttonClicked:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger slotBG = [prefs integerForKey:@"integerKey"];

    if(slotBG == 1){
        UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
        [deleteMessage show];          
    }

//对于我的警报视图

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"YES"]){
        // I KNOW THIS IS SOMEWHAT WRONG BECAUSE OF THE SENDER having errors with it
        UIButton *button = (UIButton *)sender;
        [button removeFromSuperview];
        [_images objectAtIndex:button.tag];
        [_images removeObjectAtIndex:button.tag];
        [_images insertObject:[NSNull null] atIndex:button.tag];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
        [fileManager removeItemAtPath: fullPath error:NULL];
        NSLog(@"image removed");
    }

谢谢您的帮助。

4

2 回答 2

1

在 clickedButtonAtIndex 函数中,您无法从单击的按钮获得任何引用,因为它是来自 UIAlertView 的回调。你能从这个函数中得到什么都与点击的 UIAlertView 本身有关。

如果要删除选中的图片,可以先将点击按钮的指针或标签存储在buttonClicked函数中。

- (IBAction)buttonClicked:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger slotBG = [prefs integerForKey:@"integerKey"];

    if(slotBG == 1){
        //  Get the pointer or tag of the clicked button
        _clickedButton = (UIButton *)sender;
        UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
        [deleteMessage show];        
    }  
}

然后你可以在 clickedButtonAtIndex 函数中使用这个指针/标签。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"YES"]){
        UIButton *button = _clickedButton;

        [button removeFromSuperview];
        [_images objectAtIndex:button.tag];
        [_images removeObjectAtIndex:button.tag];
        [_images insertObject:[NSNull null] atIndex:button.tag];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
        [fileManager removeItemAtPath: fullPath error:NULL];
        NSLog(@"image removed");
    }

    //  Remember to set it to nil when you finish
    _clickedButton = nil;
}
于 2012-06-20T04:07:30.877 回答
0
- (IBAction)buttonClicked:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSInteger slotBG = [prefs integerForKey:@"integerKey"];

    if(slotBG == 1){
        //  Get the pointer or tag of the clicked button
        _clickedButton = (UIButton *)sender;
        UIAlertView *deleteMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
deleteMessage.tag=1;

        [deleteMessage show];        
    }  
}

///////

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

   if (alertView.tag==1) {
    if (buttonIndex==1) {
        UIButton *button = _clickedButton;

        [button removeFromSuperview];
        [_images objectAtIndex:button.tag];
        [_images removeObjectAtIndex:button.tag];
        [_images insertObject:[NSNull null] atIndex:button.tag];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
        [fileManager removeItemAtPath: fullPath error:NULL];
        NSLog(@"image removed");
    }
}
    //  Remember to set it to nil when you finish
    _clickedButton = nil;
}
于 2012-06-20T05:08:34.850 回答