【问题标题】:IOS Issue Accessing Class PropertiesIOS 问题访问类属性
【发布时间】:2013-05-09 14:02:36
【问题描述】:

我有一个以编程方式加载为子视图的视图。该视图具有在 .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 实例?我该如何解决这个问题?

【问题讨论】:

  • 一个问题,如果您将它们声明为强而不是复制,它在私有方法中是否有效?
  • 我相信您不想要 UIViewController 的副本 - 您想要对它的弱引用。

标签: iphone ios objective-c ios6.1


【解决方案1】:

一些想法:

首先,假设您正在使用最新版本的 XCode,您不再需要声明 @synthesize 语句。编译器会自动为您插入合成语句,并创建附加下划线的实例变量(就像您所做的那样)。

其次,当您说您“无法访问初始化函数之外的属性”时——您的意思是当您尝试访问属性时您获得了错误的访问权限,或者它们只是返回 nil?我不确定你的意思是不是你说的那样,因为查看当前的初始化方法,您当前根本不访问属性,只访问它们的实例变量。

我认为您可能会混淆实例变量和属性。 Objective-C 中的一个属性如下所示:

NSLog(@"%@", self.mainImage);

...您的应用程序结构也让我有些困惑。你的初始化方法是一个实例,而不是类,方法,所以你创建一个ImageViewer,然后让它创建另一个ImageViewer。我建议您尝试创建一个真正的 init 方法,该方法调用 initWithFrame。我想您可能会发现查看 Apple 的 Objective-C 简介文档会有所帮助,因为我认为您的应用程序的结构并不能帮助您调试问题。

【讨论】:

  • 我阅读了 UIView 的文档。我明白你对类和实例之间混淆的意思。我不知道 initWithFrame 是 UIView 的初始化方法。我基本上已经将代码从加载图像复制到了 initWithFrame 方法。现在我有几个问题,文档说 initWithFrame 方法可以自定义或过度我可以将我的 ow 参数添加到 initWithFrame 方法吗?当我使用 addSubview 从主视图控制器添加视图时,是否还会调用 initWithFrame 方法? @lxt
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多