我正在尝试创建一个 UIImageView
但我必须以编程方式创建它并且我必须能够使用实例变量声明它(在 .h
文件中或类似的东西)。这是创建它的代码;但是,这不允许我在其他方法中使用它。
UIImageView *airImage = [[UIImageView alloc]
initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
我看过其他人提出类似的问题,但是没有人允许我创建实例变量。顺便说一句,该代码在我的 viewDidLoad 中。提前致谢!
最佳答案
在你的 .h 中使用:
UIImageView *airImage;
在你的viewDidLoad中:
airImage=[[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:airImage];
或者您可以将其声明为属性:
@property (nonatomic, strong) UIImageView *airImage;
并使用它来访问它:
self.airImage = [[UIImageView alloc] initWithFrame:CGRectMake(29, 7, 82, 96)];
[myScrollView addSubview:self.airImage];
关于ios - 以编程方式创建的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22493856/