IOS问题访问类属性
我有一个视图以编程方式加载为子视图。该视图在.h中定义了三个属性,并在.m中进行了合成。该视图是通过在父控制器中创建视图的对象并随后调用它的初始化函数来加载的。在初始化函数中,我为它们各自的属性设置了许多值。出于某种原因,我无法访问初始化函数之外的属性。我的第一个想法是,这些属性的定义不正确,但是在弄乱它们的强弱属性之后,没有任何变化。IOS问题访问类属性
了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实例?我怎样才能解决这个问题?
的几点思考:
首先,你不需要再申报@synthesize
语句,假设你在最近版本的Xcode工作。编译器会自动为你插入合成语句,并创建一个带有下划线的实例变量(如你所做的那样)。其次,当你说'你不能访问初始化函数之外的属性' - 你的意思是当你试图访问属性时,你会得到一个不好的访问,或者他们只是返回nil?我不确定你说的是什么意思,因为看看你当前的初始化方法,你当前没有访问属性,只有它们的实例变量。
我想你可能会得到实例变量和混合的属性。在Objective-C的属性看起来是这样的:
NSLog(@"%@", self.mainImage);
...您的应用程序结构也有点迷惑了我。您的初始化方法是一个实例,而不是类,方法,因此您只创建一个ImageViewer
,然后再创建另一个ImageViewer
。我建议你尝试创建一个真正的init方法,其中一个调用initWithFrame
。我想也许你可能会发现浏览Apple的介绍Objective-C文档很有帮助,因为我认为你的应用程序的结构不能帮助你调试问题。
我读了UIView的文档。我明白你在课堂和实例之间的混淆意味着什么。我不知道initWithFrame是UIView的初始化方法。我基本上已经将代码从加载映像复制到initWithFrame方法的视图中。现在我有几个问题,文档说initWithFrame方法可以定制或overdid我可以将我的ow参数添加到initWithFrame方法吗?当我使用addSubview从主视图控制器添加视图时,initWithFrame方法也会被调用吗? @lxt – ScottOBot 2013-05-09 18:31:38
一个问题,如果你宣布他们强大而不是复制它的私人方法工作? – Radu 2013-05-09 14:16:33
我相信你不想要UIViewController的副本 - 你想要一个弱引用。 – 2013-05-09 14:21:38