问题描述
如果我通过Storyboard创建UIImageView
并将其作为@property
添加到ViewController.h
中,则可以通过[self UIImageView]
或self.UIImageView
从ViewController.m
中的任何位置访问该UIImageView
. /p>
但是,如果我通过viewDidLoad
以编程方式创建UIImageView
,如下所示,我似乎会失去从viewDidLoad
外部访问它的能力.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
因此,如果以后要修改从viewDidLoad
创建的任何UIImageView
中显示的图像,我似乎无法访问它.有人知道我在做什么错吗?
您需要在头文件中创建UIImageView,然后在其余的视图中将其使用.不过,您需要将其添加到viewDidLoad中的视图中.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
If I create a UIImageView
via Storyboard and add it as a @property
into a ViewController.h
, I can access that UIImageView
from anywhere in the ViewController.m
via [self UIImageView]
or self.UIImageView
.
But If I create the UIImageView
programmatically from viewDidLoad
as you see below, I seem to lose the ability to access it from outside viewDidLoad
.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
So later on if I want to modify the image that is being displayed in any one of the UIImageView
I've created from viewDidLoad
, I can't seem to access it. Does anyone know what I'm doing wrong?
You would need to create your UIImageView in your header file and then it would be available throughout the rest of the view. You will need to add it to the view in viewDidLoad though.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
这篇关于如果以编程方式创建,则无法访问UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!