简体   繁体   中英

Add and control UIImageView Subview from another class

I have two classes: MainViewController and PlayerImageController (NSObject)

How would I be able to add the subview of my UIImageView from PlayerImageController to my MainViewController and dictate actions like

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:[PlayerImageController addPlayerImage]];
}

- (void)somethingHappened
{
    [PlayerImageController changePlayerImage];
}

and have my methods in my PlayerImageController class like

+ (UIImageView *) addPlayerImage
{
    heroPlayerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hero-still.png"]];
    [heroPlayerImageView setFrame:CGRectMake(151, 200, 17, 23)];
    return heroPlayerImageView;
}

+ (void) changePlayerImage 
{
  //change image
}

Thanks!

I think in your case you should use the Delegate pattern. Declare:

@protocol PlayerImageUpdater
  - createPlayerImage;
  - changePlayerImage;
@end

Then add:

@interface PlayerImageController <PlayerImageUpdater>

then add to MainViewController ivar and property like:

@property (...) id<PlayerImageUpdater> playerDelegate;

set this delegate like: mainViewController.playerDelegate = playerImageControllerInstance;

and use in code:

[playerDelegate createPlayerImage];
[playerDelegate changePlayerImage];

On one hand, I would not recommend using class methods but instance methods. This way, you could implement as many instances of your class as you need and keep a reference to your instances to update them.

On the other hand, if the UIImageView is the important attribute of your class, I suggest you implement it as a UIView subclass (if it is not, you can do it as an NSObject subclass as well, and get its UIImageView attribute).

Have a look at the following code:

PlayerImageController.h:

@interface PlayerImageController : UIView{
    UIImageView *_heroPlayerImageView;
}

-(void) changePlayerImage;

PlayerImageController.m:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _heroPlayerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hero-still.png"]];
        // x = 0 and y = 0 because its relative to its parent view, the object itself.
        [_heroPlayerImageView setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:_heroPlayerImageView];
    }
    return self;
}

MainViewController.h:

#import "PlayerImageController.h"

@interface MainViewController : UIViewController{
    PlayerImageController *_player;
}

MainViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _player = [[PlayerImageController alloc] initWithFrame:CGRect(151, 200, 17, 23)];
    [self.view addSubview:_player];
}

- (void)somethingHappened
{
    [_player changePlayerImage];
}

I hope it can help you (I haven't actually tried the code above, it could have some syntax errors).

If you are not using ARC, remember to retain and release your variables! Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM