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 7733955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:07:45+00:00 2026-06-01T07:07:45+00:00

First off I am still new to objective-c and still trying to learn as

  • 0

First off I am still new to objective-c and still trying to learn as much as I can so please bear with me.

Right now I have a UIButton that I’ve created programmatically. When the button is pressed an UIActionSheet is brought up with the choice of “Camera,” “Choose Photo” or “Cancel.” The button image is then suppose to change to the image taken (if camera was chosen) or image picked (if the camera roll was chosen).

My problem is the button’s image is not changing after UIImagePicker is dismissed. At first the button was created as a subView of a tableView and when I scrolled the tableView, it refreshed the button and the image changed (this is not how I wanted it to behave of course). However if the user decides they want to change or remove the image, they just push the button again and the UIActionSheet displays a third choice to “Remove Photo.” After scrolling the tableView I realized the button image was not changing at all, but instead a new button was created on top of the old button so I move the UIButton code to viewDidLoad instead (based upon some information found here on stackoverflow). I have also removed the ability for the tableView to scroll as I did not need scrolling for it.

I’ve been trying to solve this problem for a few days now how to refresh either the button, or the view properly after either UIImagePicker is dismissed or UIActionSheet is dismissed. I have tried using setNeedsLayout and setNeedsDisplay but these have not worked to fix my problem (I might be putting these in the wrong place). Hopefully someone has insight on this and can guide me on the proper way to make this work. Also provided is my code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Creates camera button and connects to camera menu UIActionSheet
    UIButton *cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cameraButton addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    cameraButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cameraButton.titleLabel.textAlignment = UITextAlignmentCenter;
    cameraButton.frame = CGRectMake(9, 230, 80, 80);
    [self.view addSubview:cameraButton];

    picturePresent = NO;
    NSLog(@"picturePresent = %@", picturePresent);
}

-(void)showActionSheet:(id)sender {
    if (picturePresent == NO) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
    else if (picturePresent == YES) {
    UIActionSheet *cameraMenuSheet = [[UIActionSheet alloc] initWithTitle:@"Add Photo" 
                                                                 delegate:self 
                                                        cancelButtonTitle:@"Cancel" 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Remove Photo", @"Camera", @"Choose Photo", nil];
    [cameraMenuSheet showFromTabBar:self.tabBarController.tabBar];
    }
}

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

    // Initialize UIImagePickerController
    if ([title isEqualToString:@"Camera"]) {
        // Camera
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.showsCameraControls = YES;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];

        [self presentModalViewController:imagePicker animated:YES];
        newMedia = YES;
    }
    else if ([title isEqualToString:@"Choose Photo"]) {
        // Photo library
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
        [self presentModalViewController:imagePicker animated:YES];
        newMedia = NO;
    }
    else if ([title isEqualToString:@"Remove Photo"]) {
    picturePresent = NO;

    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.view setNeedsDisplay];
    }

// Method for saving image to photo album
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    UIImage *scaledImage = [image scaleToSize:CGSizeMake(80.0, 80.0)];

        if (newMedia == YES) {
        // Save image
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

        }

        resizedImage = scaledImage;
    }

    picturePresent = YES;
    [self dismissModalViewControllerAnimated:NO];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

Additionally, now that I have moved my UIButton coding to viewDidLoad I am unsure of where to put this piece of coding:

{
    if (picturePresent == NO) {

        [cameraButton setTitle:@"Add\nPhoto" forState:UIControlStateNormal];
    }

    else if (picturePresent == YES) {

        [cameraButton setImage:resizedImage forState:UIControlStateNormal];
    }
}

Also setImage and setBackgroundImage were both used and still not working how I want it to. Let me know if more information is needed, thanks!

EDIT:

Here are some screenshots to show what I am aiming for –

screen1

screen2

screen3

“Picture” is suppose to represent a picture that was taken or selected and then scaled to fit the button size.

  • 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-01T07:07:47+00:00Added an answer on June 1, 2026 at 7:07 am

    Did not test it, but if you make your cameraButton member of your UiViewController so that you can access whenever needed, you could call the setImage: forState: right after caculating the resizedImage – no picturePresent bool needed. And I think setNeedsDisplay Not needed as well.

    EDIT:

    In your view controller header file do something like

    ...
    @interface MyViewController : UIViewController
    {
       ...
       UIButton * cameraButton;
       ...
    }
    ... 
    

    Somewhere in your implementation file (viewDidiLoad is Ok) do:

    ...
    cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
    ...    
    [self.view addSubview:cameraButton];
    ...
    

    And then implement all the other stuff you did and change the code in didFinishPickingMediaWithInfo like this:

     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
    
       ...
      [cameraButton  scaledImage forState:UIControlStateNormal];
    }
    

    As I said, didn’t test it, but I think it should work.

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

Sidebar

Related Questions

First off, please let me say I'm pretty new to CSS. Still lots to
First off, this is my first project using SQLAlchemy, so I'm still fairly new.
First off, I would like to make clear, that I am SUPER NEW TO
First off, I've never used GWT before. I have good experience in HTML/CSS/JS/JSP. I'm
First off, I'm using XCode 4.0.2. Okay, here is my issue. I can build
First off, i'm fairly new to programming, I've built a few asmx web services
First off, I'm completely new to Drupal, so I could be making a ridiculously
New to Stackoverflow, so first off, hello. I'm working on a little project for
Im still fairly new to Django, so please explain things with that in mind.
I have a PHP results page which starts off first-pass with ALL rows returned.

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.