c11ada
c11ada

Reputation: 4334

Objective C setCenter on Image

I am fairly new to iOS development. I have the following code

 @property (nonatomic, strong) NSMutableArray *myImages;
    -(void) viewDidLoad
    {
        [super viewDidLoad];

        self.myImages = [[NSMutableArray alloc] init];

        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1"]];
[self.myImages addObject:image];
        [image setCenter:CGPointMake(10, 10)];
        [self.view addSubview:image];
    }

    -(void) viewDidAppear:(BOOL)animated
    {
            for (;;) {
                for (int i=0; i<[self.myImages count]; i++) {
                    [self.myImages[i] setCenter:CGPointMake(300, 300)];
                }
            }

    }

When I do this, the image shows up at point 10,10. However it doesn't get changed to 300,300. Can any one suggest what I am doing wrong.

If I do something like

[self.myImages[0] setCenter:CGPointMake(300, 300)];

before the infinite loop, that works fine. but in the infinite loop no luck.

This code is just a snippet and a lot of the code is missing, but you should be able to understand what I am getting at with this.

Thanks

Upvotes: 0

Views: 247

Answers (2)

gal.orlanczyk
gal.orlanczyk

Reputation: 131

first i'm kinda new to ios too so i am not sure.

i think there are 2 options:

  1. i am not sure if view did appear is the right method to change the image i would suggest view will appear. and if you really want the one you used maybe try dispatch_async in order to change the image on the main ui thread.

  2. Maybe try for in loop? that why you will set itrator as UIimage and it will recognize the image. this is less likly to solve it but maybe...

Upvotes: 0

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

You forgot to add the imageView to the array. Add

[self.myImages addObject:image];

... in viewDidLoad.

As a side note: It's not common to abbreviate identifiers in Objective-C. Use imageView instead of image. Code becomes much easier to read.

You probably don't want the infinite loop in viewDidAppear, too.

Upvotes: 4

Related Questions