How to display an image using UIView in XCode?

15,843

Solution 1

Referring to your actual problem, you're getting this exception being thrown:

2012-01-26 22:04:40.728 Switch-a-Switch[548:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SwitchViewController 0x6840660> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UIImageView.'

It would appear that you've wired up the UIImageView to a property called UIImageView rather than imageView. Go and redo the connection in Interface Builder and make sure you drag from the imageView property to your UIImageView instance.

Solution 2

UIImage *plate1 = [UIImage imageNamed:@"plates1.tif"];
[imageView setImage:plate1]; 

should be kosher in iOS and 64 Bit (non-fragile ABI environments, as the iVar will be synthesized), I think it is much better form to include the ivar in the interface....

@interface SwitchViewController : UIViewController
{
    UIImageView *imageView;
}

The thing that you should do to track this down is to turn on zombies and enable an exception breakpoint.

Share:
15,843
Ashin Mandal
Author by

Ashin Mandal

UI / UX Enthusiast

Updated on June 04, 2022

Comments

  • Ashin Mandal
    Ashin Mandal almost 2 years

    Absolutely beginner question. I am trying to make an app which will switch an array of images by swiping the screen sideways. So, as vanilla case of that I was trying to display an image first and then think about how to switch between images using an action. I am using XCode 4.2.

    So, here's what I have so far. I have added a UIImageView to my storyboard, then Ctrl+dragged into the ".h" file to create an Outlet and it looks something like this:

    SwitchViewController.h

    #import <UIKit/UIKit.h>
    
    @interface SwitchViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    
    @end
    

    And then in the ".m" file, I am trying to set an image to the UIImageView in the DidLoad method.

    SwitchViewController.m

    #import "SwitchViewController.h"
    
    @implementation SwitchViewController
    @synthesize imageView;
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        UIImage *plate1 = [UIImage imageNamed:@"plates1.tif"];
        [imageView setImage:plate1];
    }
    
    - (void)viewDidUnload
    {
        [self setImageView:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
    
            (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
    
            return YES;
    
    
    
        return NO;
    }
    
    @end
    

    Now, if I try to run this, there are no compilation errors.
    But the program halts saying, Thread 1:Program received signal "SIGABRT"