gpt4 book ai didi

objective-c - 从另一个类添加和控制 UIImageView subview

转载 作者:行者123 更新时间:2023-11-28 23:07:53 26 4
gpt4 key购买 nike

我有两个类:MainViewController 和 PlayerImageController (NSObject)

我如何才能将我的 UIImageView 的 subview 从 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
}

谢谢!

最佳答案

我认为在你的情况下你应该使用委托(delegate)模式。声明:

@protocol PlayerImageUpdater
- createPlayerImage;
- changePlayerImage;
@end

然后添加:

@interface PlayerImageController <PlayerImageUpdater>

然后添加到 MainViewController ivar 和属性,如:

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

将此委托(delegate)设置为:mainViewController.playerDelegate = playerImageControllerInstance;

并在代码中使用:

[playerDelegate createPlayerImage];
[playerDelegate changePlayerImage];

关于objective-c - 从另一个类添加和控制 UIImageView subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8988507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com