2012-06-20 77 views
0

所以我有一个UIScrollviewUIImageView集的一个按钮,我希望能够每当图像被点击,如果选择YES,然后该图像将在NSDocumentDirectory被删除的alertView将弹出。我设法使alertView出现,但图像不删除,因为我认为发送了错误的sender或button.tag。这里是我的代码:alertView没有响应

//我滚动视图

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];   
    } 

//我AlertView

- (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"); 
    } 

感谢您的帮助。

+1

第一:'如果([标题isEqualToString:@ “YES”]){'是不必要的开销。为什么不简单地使用'if(buttonIndex == 1){'?其次,从哪里检索“发件人”?没有参数alertView:clickedButtonAtIndex:'方法名为'sender' ... – 2012-06-20 03:38:58

+0

我真的不知道先生,因为以前我只是使用删除部分在 - (IBAction)buttonClicked:(id)sender'部分。但我想添加一个alertView,所以有一个确认。现在我有点搞砸了, – Bazinga

+0

但它甚至编译?我怀疑。 – 2012-06-20 03:45:39

回答

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; 
} 
+0

感谢您的帮助。 – Bazinga

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; 
}