Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2010, 05:52 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 34
Sachelis is on a distinguished road
Default Unable to set UIImageView with UIImage

<Newbie here...>

My UIViewController interface contains a UIImageView pointer.

Code:
IBOutlet UIImageView *imageFlag;
The image (which displays a country flag) is set up as a property:

Code:
@property (nonatomic, retain) UIImageView *imageFlag;
And is synthesized.

Code:
@synthesize imageFlag;
In the Interface Builder, I've added an image and connected it to the File's Owner (to the imageFlag variable).

The image appears when the UIViewController is displayed. However, when I try to change the image, it fails:

Code:
UIImage *image = [UIImage  imageNamed:@"USA.gif"];
assert( image != nil ); // This image is **not** nil.
[imageFlag setImage:image];
assert( self.imageFlag != nil ); The imageFlag var **is** nil here.
After hours of inspecting, I'm stumped. I have several other controls on this UIViewController that were set up similarly and their contents can be changed from within the code (although they are all UILabels rather than UIImageViews). I've also have a custom table cell setup elsewhere in the app that uses a similar technique to change the image that does work. I can't figure out what I'm doing wrong...

Ideas? Help? Thanks!
Sachelis is offline   Reply With Quote
Old 09-10-2010, 06:42 PM   #2 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

I don't see why that wouldn't work, unless you did not connect up the outlets correctly. Are you able to do anything to the image view, like changing its size? If you can't do anything to it, then you probably didn't connect it correctly.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-10-2010, 07:11 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sachelis View Post
<Newbie here...>

My UIViewController interface contains a UIImageView pointer.

Code:
IBOutlet UIImageView *imageFlag;
The image (which displays a country flag) is set up as a property:

Code:
@property (nonatomic, retain) UIImageView *imageFlag;
And is synthesized.

Code:
@synthesize imageFlag;
In the Interface Builder, I've added an image and connected it to the File's Owner (to the imageFlag variable).

The image appears when the UIViewController is displayed. However, when I try to change the image, it fails:

Code:
UIImage *image = [UIImage  imageNamed:@"USA.gif"];
assert( image != nil ); // This image is **not** nil.
[imageFlag setImage:image];
assert( self.imageFlag != nil ); The imageFlag var **is** nil here.
After hours of inspecting, I'm stumped. I have several other controls on this UIViewController that were set up similarly and their contents can be changed from within the code (although they are all UILabels rather than UIImageViews). I've also have a custom table cell setup elsewhere in the app that uses a similar technique to change the image that does work. I can't figure out what I'm doing wrong...

Ideas? Help? Thanks!
I think Kelvin is on to something. If the outlet to imageFlag is not connected, imageFlag would be bank, and the setImage call would be "dropped on the floor".

Set a breakpoint on the setImage line in the debugger, and make sure imageFlag is not nil.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2010, 07:53 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 34
Sachelis is on a distinguished road
Default

Quote:
Originally Posted by kelvinkao View Post
I don't see why that wouldn't work, unless you did not connect up the outlets correctly. Are you able to do anything to the image view, like changing its size? If you can't do anything to it, then you probably didn't connect it correctly.
Yeah, per your and Ducan's comments, it wasn't set up correctly. Seeing that imageFlag was nil was the key. After 20 years using C++ on that other operating system, I'm struggling with the XCode/Interface interface. Thanks for the help, guys.
Sachelis is offline   Reply With Quote
Old 09-10-2010, 08:10 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Sachelis View Post
Yeah, per your and Ducan's comments, it wasn't set up correctly. Seeing that imageFlag was nil was the key. After 20 years using C++ on that other operating system, I'm struggling with the XCode/Interface interface. Thanks for the help, guys.
It took me a while to get my head around the idea that sending a message to a nil object pointer is perfectly ok. In most other languages this causes a crash, but it's perfectly okay in Objective C.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-10-2010, 09:00 PM   #6 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Quote:
Originally Posted by Duncan C View Post
It took me a while to get my head around the idea that sending a message to a nil object pointer is perfectly ok. In most other languages this causes a crash, but it's perfectly okay in Objective C.
Yeah, I used to write a lot of code like
Code:
if(someString != nil && [someString length] > 0)
   ...  //do something here
I no longer do that.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-11-2010, 11:04 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 34
Sachelis is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
It took me a while to get my head around the idea that sending a message to a nil object pointer is perfectly ok. In most other languages this causes a crash, but it's perfectly okay in Objective C.
Yeah, I am used to the compiler (and language) making sure I'm doing what I intended to do (e.g., not passing a message to a nil pointer, not calling a member function that doesn't exist, etc). And since I don't know what I'm doing on this platform, it's a little more difficult to learn by trial-and-error. That said, I am making progress and appreciate the help that folks (like you) give to newbies on the forums. Thanks again.
Sachelis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 340
13 members and 327 guests
bignoggins, carlandrews, cgokey, flamingliquid, givensur, hzwegjxg, ilmman, jenniead38, linkmx, mraalex, PixelInteractive, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0