gpt4 book ai didi

ios - 无法在另一个类的自定义类上设置 UIImageView.image

转载 作者:行者123 更新时间:2023-11-29 00:57:59 24 4
gpt4 key购买 nike

我有一个自定义类 A,它是 UIView 的子类,它本质上管理一个适合 View 大小的 UIScrollView 并且包含一个 UIImageView 填充那个 scrollView。

我在设置自定义类的 imageView.image 时遇到问题:

这是类

#import "BackgroundPickerView.h"

@implementation BackgroundPickerView

@synthesize scrollView;
@synthesize imageView;
@synthesize actionButton;

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

return self;
}

-(void)layoutSubviews
{
[super layoutSubviews];

//[[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(changeImage)
// name:@"ImageChangeNotification"
// object:nil];

//Here's where we add custom subviews
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
actionButton = [[UIButton alloc] init];

[scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[imageView setFrame:CGRectMake(0, 0, 321, 115)];
[actionButton setFrame:CGRectMake(0, 0, 320, 115)];

scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 6.0;
scrollView.contentSize = CGSizeMake(300, 200);//imageView.image.size;

[scrollView addSubview:imageView];
[self addSubview:scrollView];
}

-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}

-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:@"random.png"]];
}

从我收到图像的其他 B 类中,我尝试使用 NSNotification 和一个简单的 setter 方法来尝试设置类对象的 imageView 属性,但似乎无法设置 imageView.image .我试过使用

instance.imageView.image = myImageToSet

//Setter
[instance storeImage:myImageToSet];

在 B 类中,但没有成功

最佳答案

首先,在 layoutSubviews 中初始化 subview 不是最好的方法,最好在 init 方法中初始化它们,因为 layoutSubviews可能会多次调用来布局 subview ,如果您将初始化代码放在 layoutSubviews 中,则可能会有 subview 。在 layoutSubviews 中,您只设置 subview 的 frame

你可以像下面那样做,

 #import "BackgroundPickerView.h" 
@implementation CustomViewA

//Edit change below

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self commonInit];
}
return self;
}

- (void)awakeFromNib
{
[self commonInit];
}

- (void)commonInit
{
_scrollView = [[UIScrollView alloc] init];
_imageView = [[UIImageView alloc] init];
_actionButton = [[UIButton alloc] init];

_scrollView.scrollEnabled = YES;
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 6.0;

[_scrollView addSubview:_imageView];
[self addSubview:_scrollView];
}

- (void)layoutSubviews
{
[super layoutSubviews];
[_scrollView setFrame:CGRectMake(0, 0, 321, 115)];
[_imageView setFrame:CGRectMake(0, 0, 321, 115)];
[_actionButton setFrame:CGRectMake(0, 0, 320, 115)];

_scrollView.contentSize = CGSizeMake(300, 200);
}

-(void)storeImage:(UIImage *)image
{
self.imageView.image = image;
}

-(void)changeImage
{
[self.imageView setImage:[UIImage imageNamed:@"random.png"]];
}

synthesese 是可选的在 BackgroundPickerView.h 文件中,它类似于下面的内容

 @interface BackgroundPickerView : UIView
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *actionButton;

-(void)storeImage:(UIImage *)image;
-(void)changeImage;

对于图像你检查它应该工作,检查图像是否存在,

编辑对于图片,以controller为例,

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_viewA = [[BackgroundPickerView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_viewA];
}

//for testing
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[_viewA storeImage:[UIImage imageNamed:@"6.png"]]; //hear do this
}

关于ios - 无法在另一个类的自定义类上设置 UIImageView.image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428071/

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