I have a UIImageView in a view of a StoryBoard, IBOutlet connections are made properly (have double and triple checked this), the images to display are added to the proper target, etc. (Have been debugging this for a day now) the UIImageView display images if I set them in Interface Builder.
But if I set the UIImageView.image property to a valid UIImage (I can see even the preview when debugging) loaded with any image it always shows the first image, not the new one, or if I left it blank in IB it keeps nil value.
This only happens in this view if I try the same thing in another view of the StoryBoard and the UIImageView properly shows the content of the UIImage.
Any clues will we greatly appreciated.
Edited:
Thanks for the replies, has tested also setting it from a button, the code is this code:
The property declaration is:
#property (strong, nonatomic) IBOutlet UIImageView *theRecomendationImage;
Have Tested with weak and strong, just to be sure.
The IBAction implementation (tested it gets called)
- (IBAction)changeImage {
UIImage *theImageToTint = [UIImage imageNamed:#"Sunny"];
// theImageToTint = [theImageToTint imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.theRecomendationImage.tintColor = [UIColor yellowColor];
self.theRecomendationImage.image = theImageToTint;
}
When debugging if you check the value of theImageToTint it is a valid image (not nil) and you can even quicklook the image. But when I assign it to the UIImageView the value is always nil. No error, just keep nil.
Thanks for your interest, and sorry for the incomplete question.
Edited:
If you execute
po self.theRecomendationImage.image
in the debug console before and after executing the assignment: self.theRecomendationImage.image = theImageToTint; the value is nil.
Edited:
If you add the UIImageView by code, it works and get updated when you assign a new image to it, this takes me to point to some Storyboard issue.
Is there any tool to verify a storyboard?
Just make sure the image format is .png instead of .jpg or any other.
Related
I used to have an image in my project and I would load it like this:
UIImage *image = [[UIImage imageNamed:#"image_name"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0f, 10.0f, 10.0f, 10.0f)];
Now I put that image into XCode 5's new asset catalog and I set the slicing for it. How do I use that image in my code so that I don't have to explicitly call resizableImageWithCapInsets when loading the image?
Said another way, how do I take the slicing information stored in Images.xcassets and store it in a UIImage's capInsets property?
Or am I thinking about this all wrong?
Ok, I was able to figure this out.
In order to automatically use the slicing information in an image stored in the asset catalog (Images.xcassets) you need to set your Deployment Target to 7.0 (or higher).
Hope this helps someone else out there.
My solution compatible with iOS 6/7 is using User Defined Runtime Attributes in Xib files. So that we don't have to write inelegant lines everywhere in the source code to replace the image set in xib with stretchable image with cap insets for the button.
Step 1: In Xib, select the button and set the User Defined Runtime Attributes in the Identity Inspector panel. You can define an attribute for setting the cap insets. For example, an attribute called "capEnabled" with just a simple boolean value to indicate we want to use default cap insets for the button. (I intended to attach screenshots but I was told I need at least 10 reputation to post image... :-( )
Step 2: Create a category on UIButton and add a property "capEnabled" and implement the getter and setter methods.
#interface UIButton (NBAHelper)
#property (nonatomic, assign) BOOL capEnabled;
#end
#implementation UIButton (NBAHelper)
-(BOOL)capEnabled{
UIImage *buttonBackgroundImage = [self backgroundImageForState:UIControlStateNormal];
CGFloat capLeft = buttonBackgroundImage ? buttonBackgroundImage.capInsets.left : 0;
return capLeft>0;
}
-(void)setCapEnabled:(BOOL)capEnabled{
if (capEnabled) {
UIImage *buttonBackgroundImage = [self backgroundImageForState:UIControlStateNormal];
if (buttonBackgroundImage) {
[self setBackgroundImage:[buttonBackgroundImage stretchableImageWithLeftCapWidth:5 topCapHeight:5] forState:UIControlStateNormal];
}
}
}
#end
Step3: Import the header file of the category everywhere you want to use the new feature for the UIButton you created or simply import it into the .pch file.
Hope my solution is helpful to you.
I want to have UILabel, but for some reason when I do theLabel.text it does not work. But my first label does.
Is there a issue in the code?
#interface ViewController : UIViewController
{
IBOutlet UILabel *Screen;
IBOutlet UILabel *Operations;
}
Then I would use it like the following:
Screen.text = [NSString stringWithFormat:#"%i", SelectNumber];
Operations.text = [NSString stringWithFormat:#"(The operation selected)"];
Make sure you have connected the two labels.
You can see that by looking at the little dots. In this case they are left to
line number 13 and 15.
If they are "filled" they have a connection.
If they are not filled like the picture below, it means there is no connection. In most cases you would have renamed the property. (Shame on apple, for not retaining the connection)
(In the picture you should notice that I have renamed the property)
One thing that can confuse people, is that even though there are no valid connection,
interface builder still says there is.
You should just click the little x button and start over.
You can do that by dragging the little dot from the property onto the interface builder object.
I am fairly new to objective-C and XCode.
I am trying to update a text label on the screen,
I call a function loadData in viewDidLoad, which works fine. I am trying to print a string that was generated in the loadData function.
After that string is generated, I use:
self.MyLabel.text=string;
But does not update.
I am using IBOutlet as well, I think it might be related to different threads but I am not sure.
Any help is appreciated.
Have you tried updating the label after the
- (void)viewDidLoad {
// call another method before assigning to retrieve the string here.
self.MyLabel.text=string;
}
Sometimes the way the UIViewController and IB setup will need you to set in after the viewDidLoad. Try NSLog(#"%#", self.MyLabel) at wherever you try to assign to see if its not null
Your statement is correct:
self.mylabel.text = #"Test";
So make sure, that the IBOutlet is correctly connected (filled grey circle).
Check also if the string contains the correct value.
I just tried to set a new image (UIImage object) for my UIButton using myBtn.imageview.image = newimg; then I found it didn't work.
After that I get the correct solution that using setImage:forState: , the documentation about UIButton says "imageView is the button’s image view. (read-only)"
How should I get it? What's the difference?
Thanks!
As the documentation says("imageView is the button’s image view. (read-only)"), it is a read only property and you cant just change the imageView. You have to use the recommended method setImage:forState:. Basically UIButton might be having a different implementation than what you are expecting and myBtn.imageview.image may not actually set the desired image to the button. setImage:forState: might be having an implementation which could be completely different from just setting myBtn.imageview.image = newimg;. By directly setting like in your question, you might get some unexpected results since the actual implementation is not executed.
I want to set an image as my background at runtime, so I created a UIImageView 'backGroundImage' as IBOutlet in Interface Builder. However the following code:
UIImage *image = [UIImage imageNamed:imageName];
NSLog(#"image=nil %d",(image ==nil));
NSLog(imageName);
[self.backGroundImage setImage:image];
NSLog(#"backgroundImage=nil %d",(self.backGroundImage.image ==nil));
gives me the following NSLogs
2012-06-15 12:09:43.967 iElster[1960:207] image=nil 0
2012-06-15 12:09:43.967 iElster[1960:207] bedroom-sketch-corel-eps-vector-art-1141.jpg
2012-06-15 12:09:43.968 iElster[1960:207] backgroundImage=nil 1
so basically I can load my image, but I can not set it as the image of the UIImageView. If I include the following line
backGroundImage = [[UIImageView alloc]init];
I don't get 'nil' anymore for my backGroundImage.image - but in that case I obviously don't use the UIImageView created by the Outlet anymore. I do not see the problem with my code above, especially since all I saw as suggestions in similar Stack Overflow threads was exactly the same: "simply use the setImage method"; Any ideas what could be the problem?
PS: just to clarify that - I tried using different images, both .png and .jpg and they all work if I e.g. just set them as a subview to my ScrollView, but not when trying to use them in the code above. So the image files shouldn't pose any problems.
It seems that you placed this code in a method that is executed before the view gets loaded and the outlets are connected (e.g., in init...). Try placing it in viewDidLoad. You cannot access your view outlets before the view gets loaded (i.e., before viewDidLoad is accessed).
Also, this line:
[image release];
is a memory management error (overrelease).