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

  • SEARCH
  • Home
  • 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 8856913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:27:57+00:00 2026-06-14T14:27:57+00:00

im trying to make an app that has 4 ImageViews and I want to

  • 0

im trying to make an app that has 4 ImageViews and I want to allow the user to change the background on the different ImageViews by pressing an UIButton below each ImageView. I have written a code for this from a tutorial showing to do this with just one ImageView. I just copied and pasted 4 times for each button and changed some variables. But when I run it only the first ImageView changes its picture even if I press UIButton for 2nd, 3rd or 4th ImageView. here the code:

#import "ViewController.h"

@interface ViewController ()
{
UIImagePickerController *imagePickerController;
UIImagePickerController *imagePickerController2;
UIImagePickerController *imagePickerController3;
UIImagePickerController *imagePickerController4;
}

@end

@implementation ViewController
@synthesize firstImageView, secondImageView, thirdImageView, fourthImageView;
- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

- (IBAction)firstChangeButton:(id)sender
{
imagePickerController = [[UIImagePickerController alloc]init];
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];

}
- (IBAction)secondChangeButton:(id)sender
{
imagePickerController2 = [[UIImagePickerController alloc]init];
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];

}
- (IBAction)thirdChangeButton:(id)sender
{
imagePickerController3 = [[UIImagePickerController alloc]init];
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];

}
- (IBAction)fourthChangeButton:(id)sender
{
imagePickerController4 = [[UIImagePickerController alloc]init];
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = @"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];

[data writeToFile:fullPathToFile atomically:YES];

[[self firstImageView]setImage:image1];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController2:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info2
{
UIImage *image2 = [info2 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data2 = UIImagePNGRepresentation(image2);
NSString *myGrabbedImage2 = @"myGrabbedImage2.png";
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory2 = [path2 objectAtIndex:0];
NSString *fullPathToFile2 = [documentDirectory2 stringByAppendingPathComponent:myGrabbedImage2];

[data2 writeToFile:fullPathToFile2 atomically:YES];

[[self secondImageView]setImage:image2];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController3:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info3
{
UIImage *image3 = [info3 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data3 = UIImagePNGRepresentation(image3);
NSString *myGrabbedImage3 = @"myGrabbedImage3.png";
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory3 = [path3 objectAtIndex:0];
NSString *fullPathToFile3 = [documentDirectory3 stringByAppendingPathComponent:myGrabbedImage3];

[data3 writeToFile:fullPathToFile3 atomically:YES];

[[self thirdImageView]setImage:image3];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController4:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info4
{
UIImage *image4 = [info4 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data4 = UIImagePNGRepresentation(image4);
NSString *myGrabbedImage4 = @"myGrabbedImage4.png";
NSArray *path4 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory4 = [path4 objectAtIndex:0];
NSString *fullPathToFile4 = [documentDirectory4 stringByAppendingPathComponent:myGrabbedImage4];

[data4 writeToFile:fullPathToFile4 atomically:YES];

[[self fourthImageView]setImage:image4];

[self dismissViewControllerAnimated:YES completion:nil];

}

@end

How should i apply it for all 4 ImageViews?
Thanks in advance!

  • 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-14T14:27:58+00:00Added an answer on June 14, 2026 at 2:27 pm

    The problem is that all image pickers will call the same delegate method:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    

    When you set the delegate for the image picker controller like so:

    [imagePickerController setDelegate:self];
    

    You basically tell the image picker controller that self will be able to respond to the image picker delegate methods.
    As you can see in the UIImagePickerControlDelegate, there’s no such thing as:

    – imagePickerController2:didFinishPickingMediaWithInfo:
    

    and so on.

    As a start, I would suggest rewriting your delegate method like this:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
    
        NSData *data = UIImagePNGRepresentation(image1);
        NSString *myGrabbedImage = @"myGrabbedImage.png";
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [path objectAtIndex:0];
        NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
    
        [data writeToFile:fullPathToFile atomically:YES];
    
        if (picker == imagePickerController) {
            [[self firstImageView]setImage:image1];
        } else if (picker == imagePickerController2) {
            [[self secondImageView]setImage:image1];
        } else if (picker == imagePickerController3) {
            [[self thirdImageView]setImage:image1];
        } else {
            [[self fourthImageView]setImage:image1];
        }
    
        [self dismissViewControllerAnimated:YES completion:nil];
    
    }
    

    This way, all image picker images will be handled by the same method, thus reducing the amount of code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make an app that has three tabs, each with different but
Right, I'm trying to make an app that has a calculation that involves a
I'm trying to make a little app that has a little meter in it
I'm trying to make a quick sample Backbone.js app that has a Post model,
I am trying to make a WP7 app that would Tweet short messages user
I am trying to make a small game like app that has some interaction,
I'm trying to make an app that has multiple pins/locations (about 50-100). Which way
I am in the process of trying to make an app that has most
I was trying to make an app which has a component that will make
I am trying to make an app that will pass data between two servers

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.