Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:49:33+00:00 2026-06-05T22:49:33+00:00

So i have a UIScrollview with UIImageView set with a button, I want to

  • 0

So i have a UIScrollview with UIImageView set with a button, I want to be able to whenever an image is clicked an alertView will pop-up if YES is selected then that image will be deleted in the NSDocumentDirectory. I manage to make the alertView appear but the image isnt deleting because I think is sending a wrong sender or button.tag. Here is my code:

//My scrollView

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

// Button

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

//for my 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");
    }

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:49:34+00:00Added an answer on June 5, 2026 at 10:49 pm

    In the clickedButtonAtIndex function you cannot get any reference from your clicked button because it is a callback from UIAlertView. What you can get insider this function are all related to the clicked UIAlertView itself.

    If you want to delete selected image, you can first store the pointer or the tag of clicked button in buttonClicked function.

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

    Then you can use this pointer/tag in the clickedButtonAtIndex function.

    - (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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a uiscrollview and there is a uiimageview inside it. I want the
I have a UIImageView that is housed inside a UIScrollView . The image in
So I have a UIImageView as a subview of UIScrollView , I've set the
In a UIScrollView I have a UIImageView with the contentMode set to Aspect Fill.
I have created a scrolling/zooming image view by placing a UIImageView within a UIScrollView.
I have paging UIScrollView with images. I want to implement animated zooming of image
I have an UIImageView with an image (map) inside of a UIScrollView. I add
I have a custom UIViewController subclass controlling a UISCrollView containing an UIImageView, set up
I have an image inside an UIImageView which is within a UIScrollView. What I
I Have a UIScrollview with a UIImageView inside where I scroll from an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.