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
Related
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.
This is a trick I use a lot in android:
I take an image with Image Picker: camera or gallery
I put the image in an ImageView
I also put the image url in the ImageView, in the tag field
so later when I need the url of an image (to send to server), I grab it from the imageView's tag.
But tag in iOS is a bit different from android: in iOS it's just a number. So is there such a way of piggybacking on an UIButton on iOS: basically any field whatsoever that is available for storing a text and which the user cannot see?
I'm not aware of an analogous field on a UIImageView. Your best bet may be to subclass UIImageView to add such a property. In the .h file for the new class, do something like:
#interface SubclassedUIImageView : UIImageView
#property (strong, nonatomic, copy) NSString *url;
#end
Then assign the url value to SubclassedUIImageView in imagePickerController: didFinishPickingMediaWithInfo:. Assuming you're using Interface Builder, to use the subclassed UIImageView, you drop a UIImageView control onto the parent view, go to the Identity inspector, and change the Custom Class field to the name of your subclassed UIImageView.
Natively, there's no way to do this. However, you can use a category and store the text in an associated object:
#interface UIImageView (StringTagAdditions)
#property (nonatomic, copy) NSString *stringTag;
#end
#implementation UIImageView (StringTagAdditions)
static NSString *kStringTagKey = #"StringTagKey";
- (NSString *)stringTag {
return objc_getAssociatedObject(self, kStringTagKey);
}
- (void)setStringTag:(NSString *)stringTag {
objc_setAssociatedObject(self, kStringTagKey, stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
#end
More info # http://nshipster.com/associated-objects/
This type of thing has been asked before, but you could pull this off with a UIButton category that adds getter/setter like methods that store this value for you:
setting new properties in category interface/implementation
Objective-C: Property / instance variable in category
http://iosdevelopertips.com/objective-c/adding-properties-category-using-associated-objects.html
Or you could subclass UIButton and add the property you need. There are lots of options, but I'm unclear on what you mean by "the user cannot see"?
I have 10 UIImageViews which do the same thing (they have some void methods that change their image with a timer).
My UIImageView is an outlet and I want to connect all the 10 imageViews to the same outlet, but interface builder doesn't allow me.
I found that there is a solution, IBOutletCollection. Can anyone explain to me how to use this to connect multiple imageViews to the same outlet?
Declare a property to hold your imageView's and then hook them up in interface builder like normal
#property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
it's just a normal NSArray but when the nib is loaded it will be populated with your imageView's
Update
In the header file for you view controller which has the multiple imageView's on you need to add the property above - it may look something like this:
#interface MyViewController : UIViewController
#property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
// other properties
#end
Now in the interface builder you connect all the imageView's to this one property.
Now I just work with the imageViews collection
for (UIImageView *imageView in self.imageViews) {
imageView.image = someImage;
}
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.
I'm getting a bit confused with UIViews recently, I have been using them fine up until this point and they are just refusing to be compliant!
I have a UIViewController, and this contains 5 different views. I have created IBOutlets for these views as I am wanting to swap them at runtime:
IBOutlet UIView *view1;
IBOutlet UIView *view2;
IBOutlet UIView *view3;
IBOutlet UIView *view4;
IBOutlet UIView *view5;
To make them easier to maintain I decided to keep them all within an array, called viewArray. Now I am trying to add the views to the array as follows:
viewArray = [NSArray arrayWithObjects:view1, view2, view3, view4, nil];
This is being called in the init function of my UIViewController class. I have linked up all the IBOutlets to their relevant views in the xib / interface file, but they do not appear to be initialized. Upon further debugging it looks like the views aren't initialized until after the init function is called?
So how can I create an array of these objects? I will need to select the relevant view before the view itself is shown, therefore viewDidLoad is not an option.
I know that you can grab the tags of things and implicitly set them using:
imageExample = (UIImageView *)[self.view viewWithTag:100];
But can this be used to find views, as surely it will be searching for the tags within the originally initialized view (view1)?
Thanks for any help in advanced,
Kind Regards,
Elliott
You can initialize the viewArray lazily, for the price of having to use self.viewArray instead of unqualified viewArray.
Here is how you can do it:
In the .h file:
NSArray* _viewArray;
#property (nonatomic, readonly) NSArray *viewArray;
In the .m file:
-(NSArray*) viewArray {
if (!_viewArray) {
_viewArray = [NSArray arrayWithObjects:view1, view2, view3, view4, nil];
}
return _viewArray;
}