我有一个以编程方式加载为子视图的视图。该视图具有在 .h 中定义并在 .m 中合成的三个属性。通过在父控制器中创建视图对象然后调用它的初始化函数来加载视图。在初始化函数中,我为它们各自的属性设置了一些值。由于某种原因,我无法访问初始化函数之外的属性。我的第一个想法是属性定义不正确,但是在弄乱了它们的强弱属性之后没有任何改变。
UIView 的 .h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <Parse/Parse.h>
@protocol subViewDelegate
- (void)photoFromSubview:(NSInteger *)imageId;
- (void)downloadData;
@end
@interface ImageViewer : UIView
@property (nonatomic, assign) id <subViewDelegate> delegate;
//three properties
@property (nonatomic, copy) UIImage *mainImage;
@property (nonatomic, copy) UIViewController *parentView;
@property (nonatomic) NSInteger imageId;
- (void)removeImageViewer;
- (void)captureImage:(id)sender;
- (void)uploadPhoto:(id)sender;
- (UIImage *)addBackground;
- (ImageViewer *)loadImageIntoViewer:(UIViewController *)superView imageToLoad:(UIImage *)imageToLoad imageId:(NSInteger *)imageId;
@end
UIViews .m 中的相关函数
//implementation of the properties
#import "ImageViewer.h"
#import "SpinnerView.h"
@implementation ImageViewer : UIView
{
}
@synthesize mainImage = _mainImage;
@synthesize parentView = _parentView;
@synthesize imageId = _imageId;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
}
return self;
}
//initialization function
- (ImageViewer *)loadImageIntoViewer:(UIViewController *)superView imageToLoad:(UIImage *)imageToLoad imageId:(NSInteger *)imageId
{
//create a new view with the same frame size as the superView
ImageViewer *imageViewer = [[ImageViewer alloc] initWithFrame:superView.view.bounds];
imageViewer.delegate = superView;
//three properties assigning values
_mainImage = imageToLoad;
_parentView = superView;
_imageId = imageId;
//if something's gone wrong, abort!
if(!imageViewer)
{
return nil;
}
//add all components and functionalities to the program
//when I use _mainImage bellow it works with the correct value
UIImageView *background = [[UIImageView alloc] initWithImage:[imageViewer addBackground]];
background.alpha = 0.85;
[imageViewer addSubview:background];
[imageViewer addSubview:[imageViewer addImageToView:_mainImage superView:superView.view]];
[imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 10.0) yPos:(superView.view.bounds.origin.x + 10.0) buttonAction:@selector(back:) buttonTitle:@"Back"]];
[imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 10.0) yPos:((superView.view.center.y/2) + 270.0) buttonAction:@selector(captureImage:) buttonTitle:@"Camera"]];
[imageViewer addSubview:[imageViewer addButtonToView:(superView.view.bounds.origin.x + 105.0) yPos:((superView.view.center.y/2) + 270.0) buttonAction:@selector(uploadPhoto:) buttonTitle:@"Upload"]];
[superView.view addSubview:imageViewer];
return imageViewer;
}
上面的代码允许您访问分配给 _mainImage、_parentView 和 _imageId 的值。但是当我尝试通过 .m 中定义的私有函数访问它们时,返回初始化值,如下所示。
- (void)uploadPhoto:(id)sender
{
//These all return as empty or uninitialized
NSLog(@"%@", _mainImage);
NSLog(@"%d", _imageId);
}
为什么是这样?我是在 .h 中错误地定义了属性,还是因为 self 没有引用 loadImageIntoViewer 中定义的 ImageView 实例?我怎样才能解决这个问题?