iphone - IOS 问题访问类属性

标签 iphone ios objective-c ios6.1

我有一个以编程方式加载为 subview 的 View 。该 View 具有在 .h 中定义并在 .m 中合成的三个属性。通过在父 Controller 中创建 View 的对象然后调用它的初始化函数来加载 View 。在初始化函数中,我为它们各自的属性设置了一些值。出于某种原因,我无法访问初始化函数之外的属性。我的第一个想法是属性定义不正确,但在弄乱了它们的 strong 和 weak 属性后,什么都没有改变。

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 中定义的私有(private)函数访问它们时,返回初始化值,如下所示。

- (void)uploadPhoto:(id)sender
{
   //These all return as empty or uninitialized
   NSLog(@"%@", _mainImage);
   NSLog(@"%d", _imageId);
}

这是为什么?我是在 .h 中错误地定义了属性,还是因为 self 没有引用 loadImageIntoViewer 中定义的 ImageView 实例?我该如何解决这个问题?

最佳答案

一些想法:

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

其次,当您说您“无法访问初始化函数之外的属性”时 - 您的意思是当您尝试访问这些属性时,您的访问权限很差,或者它们只是返回 nil?我不确定你说的是不是真的,因为查看你当前的初始化方法,你目前根本不访问属性,只访问它们的实例变量。

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

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

...您的应用程序结构也让我有些困惑。您的初始化方法是一个实例,而不是类方法,因此您创建一个 ImageViewer 只是为了让它创建另一个 ImageViewer。我建议您尝试创建一个真正的 init 方法,一个调用 initWithFrame 的方法。我认为您可能会发现浏览 Apple 的 Objective-C 介绍文档会有所帮助,因为我认为您的应用程序的结构无法帮助您调试问题。

关于iphone - IOS 问题访问类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463640/

相关文章:

ios - Objective-C block 。上下文捕获相同的变量/复制相同的 NSStackBlock

objective-c - 当有人在 Objective-C 类/对象中使用特定方法时限制使用/生成编译错误

iphone - 在 iPhone/iPad 通用应用程序中管理图像

ios - “???”显示在 iOS 设备上的字符串中,但不是模拟器

iphone - NSDate 格式输出错误的日期

ios - 在 Objective-C 的 TableView 中给出没有任何部分的部分索引以对单个 NSArray 进行排序?

ios - 在 Objective-C 中向不同对象发送消息的最佳方式

iphone - iOS 获取已安装的配置文件

ios - 如何防止 iOS 自动删除为离线播放而保存的 HLS 内容?

ios - UIWebView 委托(delegate)不更改其他 View