我有两个类:MainViewController和PlayerImageController(NSObject)
我如何能够将UIImageView的子视图从PlayerImageController添加到MainViewController并规定诸如
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[PlayerImageController addPlayerImage]];
}
- (void)somethingHappened
{
[PlayerImageController changePlayerImage];
}
并将我的方法放在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
}
谢谢!
最佳答案
我认为在您的情况下,您应该使用代理模式。
宣布:
@protocol PlayerImageUpdater
- createPlayerImage;
- changePlayerImage;
@end
然后加:
@interface PlayerImageController <PlayerImageUpdater>
然后添加到MainViewController ivar和属性,如:
@property (...) id<PlayerImageUpdater> playerDelegate;
将此代表设置为:
mainViewController.playerDelegate = playerImageControllerInstance;
并在代码中使用:
[playerDelegate createPlayerImage];
[playerDelegate changePlayerImage];