프로그래밍 방식으로 하위보기로로드 된보기가 있습니다. 뷰에는 .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의 관련 기능은 위의 코드는 당신이 _mainImage, _parentView 및 _imageId에 할당 된 값에 액세스 할 수 있습니다
//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;
}
하는 .m. 그러나 .m에서 정의 된 private 함수를 통해 액세스하려고하면 아래와 같이 초기화 된 값을 반환합니다.
- (void)uploadPhoto:(id)sender
{
//These all return as empty or uninitialized
NSLog(@"%@", _mainImage);
NSLog(@"%d", _imageId);
}
왜 그렇습니까? 내가 속성을 .h에서 잘못 정의했거나 자체가 loadImageIntoViewer에 정의 된 ImageView의 인스턴스를 참조하지 않았기 때문입니까? 이 문제를 어떻게 해결할 수 있습니까?
개인용 방법으로 복사하는 대신 강력한 것으로 선언하면 한 가지 질문이 생깁니 까? – Radu
나는 당신이 UIViewController의 사본을 원하지 않는다고 믿는다 - 당신은 그것에 대한 약한 참조를 원한다. –