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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:27:45+00:00 2026-05-26T08:27:45+00:00

I want to have a pointer to an UIImage view so that I don’t

  • 0

I want to have a pointer to an UIImage view so that I don’t have to repeat a lot of my code. I need to choose the image based on which button it is but then at the end I apply the same code to whatever digit. oneDigit, twoDigit, and threeDigit are all @property(nonatomic, retain) in the header file. I want imageView to be able to point to one of these UIImageViews. Right now the UIImageViews are not being removed with removeFromSuperview because imageView is not pointing to the objects but creating its own object..

Currently when a button is pressed a Image is added. I want the previous image removed and the new image drawn according to what button was pressed. The code works how I want if I replace imageView with oneDigit, but that would require me to add more code for each variable.

See simplified example below:

- (void)viewDidLoad
{   ...
    UIImageView *oneDigit;
    UIImageView *twoDigit;
    UIImageView *threeDigit;
}

-(IBAction)pressButton:(id)sender {
    UIImage *image;
    UIImageView *imageView;
    UIButton *btn = (UIButton*)sender;
    int value = [btn.titleLabel.text intValue];
    switch (value) {
        case 10:
        case 15:
        case 20:
            [twoDigit removeFromSuperview]; //Remove old image from screen
            imageView = twoDigit; //Update imageView pointer for current case
            image = [UIImage imageNamed:@"twoDigit.png"];
            break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            [oneDigit removeFromSuperview];
            imageView = oneDigit;
            image = [UIImage imageNamed:@"oneDigit.png"];
            break;
        case 100:
        case 200:
        case 300:
            [threeDigit removeFromSuperview];
            imageView = threeDigit;
            image = [UIImage imageNamed:@"threeDigit.png"];
            break;
        default:
            break;
    }
    CGPoint point = btn.frame.origin;
    CGFloat xposition = point.x - ((image.size.width - btn.frame.size.width) /2);
    CGFloat yposition = point.y - ((image.size.height - btn.frame.size.height) /2);
    imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(xposition, yposition, image.size.width, image.size.height) ];
    imageView.image = image;
    imageView.contentMode = UIViewContentModeCenter;
    [self.view addSubview:imageView];
}
  • 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-05-26T08:27:46+00:00Added an answer on May 26, 2026 at 8:27 am

    Assuming that oneDigit, twoDigit and threeDigit are UIImageViews, you can just do something similar to

    imageView = oneDigit;
    

    Since both are pointers, setting imageView to oneDigit will simply point imageView to the location of oneDigit. When you set it to the new image, don’t autorelease it first, because if it’s set to threeDigit for example, it will autorelease threeDigit too.

    Also, you don’t need to set the image property of imageView manually after setting imageView to the new image, because it’s essentially referencing the exact same UIImageView with the same image; it’s just under a different name.

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

Sidebar

Related Questions

I have a pointer to a array of pointers that I want to be
Suppose I have some pointer, which I want to reinterpret as static dimension array
I have a void pointer returned by dlsym(), I want to call the function
i want have an image and i want to set it as a background
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
I want to have a map of vectors, (but I don't want to use
I want to have a map that has a homogeneous key type but heterogeneous
let's say I have a pointer to some base class and I want to
I have a C function that takes a function pointer as argument, it's a

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.