I am trying to create a UIImageView but I have to make it programmatically and I have to be able to declare it with an instance variable (in the .h file or something of the sort). Here is the code for creating it; however, this does not allow me to use it in other methods.
UIImageView *airImage = [[UIImageView alloc]
initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
I have looked on other people asking similar questions however none of them will allow me to create an instance variable. BTW that code is in my viewDidLoad. Thanks in advance!
In your .h use:
UIImageView *airImage;
In your viewDidLoad:
airImage=[[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
Or you can declare it as a property:
#property (nonatomic, strong) UIImageView *airImage;
and use to access it:
self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
To be more specific, instance variables should be created in a specific place in an interface (can be both in your .h and .m files, but use .h as it is more common).
If you want to declare it in your .h file, then you will want your code to look like this:
#interface ClassName : UIViewController {
UIImageView *_airImage; //many developers use _ to represent ivars
}
#end
To set the value of the variable, then you can use
_airImage = [[UIImageView alloc]init...];
Property's are another option. Instead, you can declare this like so:
#interface ClassName : UIViewController
#property (strong, nonatomic) UIImageView *airImage;
#end
To set this value, simply use,
self.airImage = [[UIImageView alloc]init...];
Hope this helped clear some things up. Use this question to help understand the difference and when to use ivars vs properties: What is the difference between ivars and properties in Objective-C
This tutorial shows how you can use both ivars and properties together, and just help you understand them both better: http://www.icodeblog.com/2011/07/13/coding-conventions-ivars/
in your .h
#property (nonatomic, strong) UIImageView *airImage; // public
in your .m (viewDidLoad or wherever you want to init your ImageView)
self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
Related
I need to connect an IBOutlet from this UIImageView to my ViewController in order to perform animations on it (just swiping a stone-like block around a grid).
However, I already have a category file that has an (+instancetype) method to create the UIImage programmatically, so I can use it to set an instance variable (usable throughout other files). Problem is (and bear with me because I am new to performing animation methods on movable objects) I can't use that instance variable (which is a UIView*) to do any animations. Actually, let me know if there is a way (to be used with the +(void)animateWithDuration.... method) because it would be beneficial.
+ (instancetype)stoneOneCreator {
UIImage* stoneOneImage = [UIImage imageNamed:#"Stone.png"];
UIImageView* stoneOneView = [[UIImageView alloc] initWithImage:
stoneOneImage];
return stoneOneView;
}
I call this method in my ViewController, set it to an IV, then use that IV throughout the other files where needed.
#implementation
{
UIView* _one;
}
_one = [UIView stoneOneCreator];
Then use _one by passing into other methods that are implemented in other files etc. etc.
My outlet property though when I'm working with the storyboard is
#property (weak, nonatomic) IBOutlet UIImageView* stoneOne;
Is there a way to use the 'stoneOne' property for IV purposes similar to '_one'? Or, is there a way to tie together the '_one' IV with the 'stoneOne' property?
Thanks,
Anthony
For some reason the image is not showing with this code:
YellowClass.h
#property (strong, nonatomic) IBOutlet UIImageView *myImage;
BlueClass.m
YellowClass *yellowClass = [[YellowClass alloc] init];
yellowClass.myImage.image = [UIImage imageNamed:#"Img.png"];
Two things. The instance of YellowClass that you create with alloc init is probably not the one you have on screen (though I can't be sure with that small snippet of code). Secondly, the view of yellowClass has not yet been loaded at the time you access its IBOutlet (myImage), so that outlet will be nil.
I am very new to iPhone programming and I have a very basic problem which just confuses me. I declare a public UIScrollView in my header file like this.
#property UIScrollView *scroller;
Then in my implementation file I synthesize it;
#synthesize scroller;
And in the
- (id)initWithFrame:(CGRect)frame
method I allocate my object.
if (self) {
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(23, 99, 280, 300)];
}
( The class is extended from UIView )
But when I try to access this property in a method I always get nil value which just does not make sense.
- (void)DeselectAll{
scroller.hidden = YES;
}
( The scroller is always returning nil here. )
I also try accessing it with self.scroller but the value is still nil. I am sure that I am missing a very simple point but just couldn't figure it out.
( By the way this problem is happening for all my public properties )
Any help is very much appreciated.
Thank you.
First of all, declare the property like below
#property(strong,nonatomic) UIScrollView *scroller;
Now you initialize as
if (self) {
self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(23, 99, 280, 300)];
}
Create the UIScrollView in your header file like so :
#interface MyViewController : UIViewController {
UIScrollView * scrollView;
}
#property (nonatomic, retain) IBOutlet UIScrollView * scrollView;
#end
Then initialize it in viewDidLoad:
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc]init(WithFrame:)];
[self.scrollView addSubview:...];
self.scrollView.contentSize = ...;
}
If you are using ARC, I think the properties are getting released.
Just use strong property at the time of declaration.
#property(strong, nonatomic)UIScrollView *scroller;
Ya you have correct. But you had missed memory management to property. Take a look at this.
For example, if you need to hold your created object. you should do this.
For ARC,
#property(strong,nonatomic) UIScrollView *scroller;
withour ARc
#property(retain,nonatomic) UIScrollView *scroller;
Update:
You must assign it using self. Otherwise it will not set via setter method. like as below
if (self) {
self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(23, 99, 280, 300)];
}
Ok, here is how i solved this problem. I created these controls in my nib file and assigned the variables as outlets to these objects. In that case the objects are retained and I can access them from my methods. (I tried defining the variables as IBOutlets without assigning to the controls in the nib file and in that case values were returned as nil again. Only after I link them to controls, the objects are retained. ) Although I solved this problem I have difficulty understanding this behaviour. I tried everything recommended in the answers but weirdly I kept getting nil for the object in my method.
Thanks to everyone that has taken time to write a reply to my question. Maybe someone can explain and help me understand this weird behaviour?
Cheers.
Have you checked that initWithFrame: is being called?
If you are creating your UIView subclass in a NIB file then that method won't be called, but initWithCoder: is called instead.
Request for member 'bunny' in something not a structure or union
Why exactly am I getting this error? My line looks like this:
[self.bunny setFrame:CGRectMake(100, 100, 100, 100)];
With a declared property as:
#property (nonatomic, strong) UIImageView *bunny;
And I init it with:
self.bunny = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
self.bunny.image = [UIImage imageNamed:#"bunny.png"];
But all the assignment lines error with that error. What am I doing wrong?
I don't see anything wrong with the code you have posted. Make sure that you haven't inadvertently declare that property as a UIImage instead of a UIImageView. If you haven't done that, please post all the code from the VC.
For other's info, in Xcode 4.4 & up, the properties are auto-synthesized to property = _property.
Ok. From the project you sent me, there were two issues:
You need to declare that property with retain directive instead of strong if you aren't using ARC (which the project you sent me isn't)
You have instantiated that class as a category, you cannot do that and have a private class variable. Either remove that category name, or declare
#property (nonatomic, retain) UIImageView *bunny;
in the header file instead.
And the implementation file:
#import "SlidingTableViewCell.h"
#import "CellBack.h"
#import "CellFront.h"
#interface SlidingTableViewCell (ShitBalst)
-(void)springBack;
-(void)toggleTableScrolling:(BOOL)canScroll;
#end
#implementation SlidingTableViewCell
#synthesize bunny;
#define kDragDist 80.0
BTW - here is the project that I updated:
https://dl.dropboxusercontent.com/u/3660978/TableViewCellSlider.zip
Have you synthesized bunny?
#synthesize bunny = _bunny;
The object self does not have a 'bunny'. Typically this is because you haven't synthesized the property. But you would see a compiler warning for that. Did you overlook the warning?
I am stuck at a simple task, I have uilabel on my uiviewcontroller's interface file. I want to update that label via some methods. It doesn't update the label.
.h
UIViewController
{
UILabel *pictureNameLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *pictureNameLabel;
.m
#synthesize pictureNameLabel=_pictureNameLabel;
- (void)viewDidLoad
{
_pictureNameLabel = [[UILabel alloc] init];
_pictureNameLabel.textColor=[UIColor blackColor];
_pictureNameLabel.text=#"Try";
}
How can I fix that issue?
You don't need to alloc the label. It's already alive and get's awaken from the nib.
.h
UIViewController
{
//UILabel *pictureNameLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *pictureNameLabel;
.m
#synthesize pictureNameLabel=_pictureNameLabel;
- (void)viewDidLoad
{
//_pictureNameLabel = [[UILabel alloc] init];
_pictureNameLabel.textColor=[UIColor blackColor];
_pictureNameLabel.text=#"Try";
}
Your direct problem is the line:
_pictureNameLabel = [[UILabel alloc] init];
In -ViewDidLoad. It's creating a new variable, and having the pictureNameLabel property point to it, causing you to lose your reference to the one created in Interface Builder. Remove that line, and everything should work fine.
If you've created an element via Interface Builder, you do not need to alloc & init it yourself, along with adding it to the view in the appropriate spot, as it's automatically done for you, and the property is already set to point to it. If you're manually creating a new view, you do need to do that... but you'd also need to add it somewhere in the view hierarchy as a subview.
Not related to your problem, but you have also created a variable named UILabel *pictureNameLabel;. I'd assume you created this variable to be the backing variable for the synthesized property pictureNameLabel... but, you synthesized that to _pictureNameLabel, which means you now have two variables, _pictureNameLabel and pictureNameLabel. This is almost certainly a mistake. You should either remove the manual definition of pictureNameLabel, or rename it to something distinct if you actually intend to use it separately from the property with the same name. Having it is likely to just lead to confusion & bugs down the road.
your label has already exists on your xib file, and you can set the textcolor, text on interface bulider.