0

I'm writing an application where I want an image to be displayed on the first page, but only on a small portion of the front page. What I've been trying to do is set up a button on a subview that will switch the image on the first view. This has had me stumped for hours. Can anyone help?

  • Just trying to understand what you're asking here ... so you've got a UIImageView (with the intended image) and you've set its frame appropriately so that it covers a 'portion of the front page' and that's working fine, yes? And next you've added a button and when you press that button it should do something (change the loaded image, or maybe hide that image, not sure what) but this is the part that's not working, right? – jarmod Jan 20 '11 at 00:47

2 Answers2

1

Assuming that your view controller has a member variable named 'myImageView' that's hooked up to the actual UIImageView, then you can change a UIImageView's image, as follows (caveat: I haven't compiled this code so it may have minor errors):

// create some images
UIImage *imgPig = [[UIImage imageNamed:@"pig.png"] retain];
UIImage *imgCow = [[UIImage imageNamed:@"cow.png"] retain];

...

// sometime later, change the imageview to show a pig
[[self myImageView] image] = imgPig;

...

// sometime later, change the pig to a cow
[[self myImageView] image] = imgCow;

// finally release images
[imgPig release];
[imgCow release];

Or is the problem that you have about how to access the actual UIImageView? Or is it that you don't know how to detect the button press so that you can actually do something in response to the button being pressed?

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • The problem is that I don't know how to access the UIImageView from a different view. I cant really hook them up in IB, so i'm not sure how to get the first view to know that the button in the second view has been pressed. That's the main problem i have – kevin Fox Jan 20 '11 at 21:19
  • Personally I'd consider designing the subview to make a callback when the button is pressed. See http://stackoverflow.com/questions/1015608/how-to-perform-callbacks-in-objective-c. – jarmod Jan 20 '11 at 21:47
0

Yes, that's right. I have three .h and .m files: appdelegate, the viewcontroller, and then a subview. What I'm trying to do is let the user change the image to a different image on the first page by using a button located on the subview. On the first view there's a label and a UIImageView (This imageview is the one i want to change). On the second view there's a UIButton. When this button is pressed, it should change the UIImageView on the first page. The problem I've been having is letting the first page know that the button has been pressed, and changing the picture accordingly. Hope thats enough info for you to help. thanks!!

[edit to guy below me: sorry, I switched comptuers and couldnt edit the original post]

kevin fox
  • 11
  • 2
  • Please don't post answers to your question to add more information; edit your question instead. Stack Overflow does not work like a typical Web forum. – Shaggy Frog Jan 20 '11 at 04:13