Reputation: 2018
I guess this is a very basic question, but anyway I don't know how to call this approach therefore I cannot find it anywhere online.
Let's say I have an object myImg
:
UIImageView *myImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImg.png"]];
And a method:
-(void)myMethod {
// access myImg and change something on it
}
How can I access myImg
object within myMethod
when I fire it:
[myImg myMethod];
Is it even possible or do I have to create a custom class for UIImageView with my custom method?
Upvotes: 0
Views: 46
Reputation: 3765
change the method to:
-(void)myMethod:(UIImageView *myImg) {
// access myImg and change something on it
}
UPDATE: You are looking for how to implement a category. See the following link, provided by apple https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html
Upvotes: -1
Reputation: 130193
If you've declared myMethod
in a category of UIImageView
then you can reference myImg
by using self
.
@interface UIImageView (Category)
- (void)myMethod;
@end
@implementation UIImageView (Category)
- (void)myMethod
{
// reference self here for reference to myImg
}
@end
Upvotes: 3
Reputation: 8200
Add a property to the class that you're declaring the method in:
@property (nonatomic, strong) UIImageView *myImg;
Then change your line where you create the UIImageView
:
self.myImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImg.png"]];
Then from your method, you can access the UIImageView
by referencing self.myImg
.
Upvotes: -1