我有一個視圖以編程方式加載爲子視圖。該視圖在.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實例?我怎樣才能解決這個問題?
一個問題,如果你宣佈他們強大而不是複製它的私人方法工作? – Radu 2013-05-09 14:16:33
我相信你不想要UIViewController的副本 - 你想要一個弱引用。 – 2013-05-09 14:21:38