Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you set up an UIImageView to point to another UIImageView
    primarykey
    data
    text
    <p>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. <code>oneDigit</code>, <code>twoDigit</code>, and <code>threeDigit</code> are all <code>@property(nonatomic, retain)</code> 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 <code>removeFromSuperview</code> because <code>imageView</code> is not pointing to the objects but creating its own object..</p> <p>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.</p> <p>See simplified example below:</p> <pre><code>- (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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload