简体   繁体   English

从另一个类添加和控制UIImageView子视图

[英]Add and control UIImageView Subview from another class

I have two classes: MainViewController and PlayerImageController (NSObject) 我有两个类:MainViewController和PlayerImageController(NSObject)

How would I be able to add the subview of my UIImageView from PlayerImageController to my MainViewController and dictate actions like 我怎样才能将我的UIImageView的子视图从PlayerImageController添加到我的MainViewController并指示像

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

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

and have my methods in my PlayerImageController class like 并在我的PlayerImageController类中使用我的方法

+ (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. 我认为在你的情况下你应该使用Delegate模式。 Declare: 宣布:

@protocol PlayerImageUpdater
  - createPlayerImage;
  - changePlayerImage;
@end

Then add: 然后加:

@interface PlayerImageController <PlayerImageUpdater>

then add to MainViewController ivar and property like: 然后添加到MainViewController ivar和属性,如:

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

set this delegate like: mainViewController.playerDelegate = playerImageControllerInstance; 将此委托设置为: 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). 另一方面,如果UIImageView是您的类的重要属性,我建议您将其实现为UIView子类(如果不是,您也可以将其作为NSObject子类来实现,并获取其UIImageView属性)。

Have a look at the following code: 看看下面的代码:

PlayerImageController.h: PlayerImageController.h:

@interface PlayerImageController : UIView{
    UIImageView *_heroPlayerImageView;
}

-(void) changePlayerImage;

PlayerImageController.m: 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: MainViewController.h:

#import "PlayerImageController.h"

@interface MainViewController : UIViewController{
    PlayerImageController *_player;
}

MainViewController.m: 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! 如果您不使用ARC,请记住保留并释放变量! Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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