2012-06-20 109 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; 
}