Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Help using one XIB for displaying Images

lukeirvinlukeirvin Posts: 269Registered Users
edited May 2012 in iPhone SDK Development
Right now I have multiple buttons set up, each are set to custom with an image.

Once the button is tapped I open a new ViewController to display the image.

I want to only use one ViewController/XIB for the images since it would be a headache to replicate this over and over and over again.

What would be the best method to implement this?

I think the difficult part is because I am using multiple buttons.

Should I set up one button and just scroll through my images and code it to know that when this image is on the button display it in the new XIB?

Or can I still set this up using the multiple buttons?

Any help is appreciated.
Post edited by lukeirvin on

Replies

  • Duncan CDuncan C Posts: 8,147Tutorial Authors, Registered Users
    edited May 2012
    lukeirvin;429603 said:
    Right now I have multiple buttons set up, each are set to custom with an image.

    Once the button is tapped I open a new ViewController to display the image.

    I want to only use one ViewController/XIB for the images since it would be a headache to replicate this over and over and over again.

    What would be the best method to implement this?

    I think the difficult part is because I am using multiple buttons.

    Should I set up one button and just scroll through my images and code it to know that when this image is on the button display it in the new XIB?

    Or can I still set this up using the multiple buttons?

    Any help is appreciated.

    UIView objects have a property call tag. It's an integer.

    Set up each button with a different, non-zero tag number.

    Have each button invoke the same action.

    In that action, check the tag number of the button, and use that to look up the image.

    Add a UIImage property to your second view controller.

    In the button action, look up the image using the tag number (from an array of images perhaps, or an array of filenames to images.) Set the image property in the second view controller and invoke it.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • lukeirvinlukeirvin Posts: 269Registered Users
    edited May 2012
    What would be the best method to check the tag number?

    Using a for loop?
  • lukeirvinlukeirvin Posts: 269Registered Users
    edited May 2012
    I have set up an NSMutableArray with the image files. I have also set a UIImage property in my Second View Controller.

    I am trying this:

    viewDidLoad:

    imageArray = [[NSMutableArray alloc] initWithObjects:@\"image1.png\", @\"image2.png\", @\"image3.png\", nil];


    In my Button Action:

    NSInteger tag;
    tag = [sender tag];

    if (tag == 0) {
    return;
    }
    else {
    NSString * imageName = [self.imageArray objectAtIndex:tag - 1];

    topImageView.image = [UIImage imageNamed:imageName];
    }


    In my Second View Controller I have this:

    topImageView.image = image;


    My image is still not getting set though.

    Thoughts?
  • lukeirvinlukeirvin Posts: 269Registered Users
    edited May 2012
    I changed to this in my main ViewController:


    NSString * imageName = [self.imageArray objectAtIndex:tag - 1];
    image = [UIImage imageNamed:imageName];
    topImageView.image = image;


    I'm able to pick up the image now. But I am still not getting it in my Second View Controller.

    Thoughts?

    I have properties set for both the UIImage and UIImageView
  • lukeirvinlukeirvin Posts: 269Registered Users
    edited May 2012
    Figured it out. Super simple mistake...

    I needed to set my secondViewControler.image = image

    Thanks for the help earlier Duncan :)
  • Duncan CDuncan C Posts: 8,147Tutorial Authors, Registered Users
    edited May 2012
    lukeirvin;430031 said:
    I changed to this in my main ViewController:


    NSString * imageName = [self.imageArray objectAtIndex:tag - 1];
    image = [UIImage imageNamed:imageName];
    topImageView.image = image;


    I'm able to pick up the image now. But I am still not getting it in my Second View Controller.

    Thoughts?

    I have properties set for both the UIImage and UIImageView
    Don't try to change another view controller's views directly. There are lots of reasons why that's bad. This is one... it doesn't work sometimes. Even in cases where it does work, it's bad design.

    Create an image property in the second view controller, and set that.

    Then, in your second view controller's viewWillAppear method, set the image view's image to the view controller's image property.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.