2013-05-09 3 views
1

프로그래밍 방식으로 하위보기로로드 된보기가 있습니다. 뷰에는 .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의 인스턴스를 참조하지 않았기 때문입니까? 이 문제를 어떻게 해결할 수 있습니까?

+0

개인용 방법으로 복사하는 대신 강력한 것으로 선언하면 한 가지 질문이 생깁니 까? – Radu

+0

나는 당신이 UIViewController의 사본을 원하지 않는다고 믿는다 - 당신은 그것에 대한 약한 참조를 원한다. –

답변

1

일부의 생각은 : 첫째

, 당신은 엑스 코드의 최신 버전에 최선을 다하고 있습니다 가정, 더 이상 @synthesize 문을 선언 할 필요가 없습니다. 컴파일러는 자동으로 synthesize 문을 삽입하고 인스턴스 변수에 밑줄이 추가되도록 만듭니다 (앞에서와 마찬가지로).

두 번째로 '초기화 기능 밖의 속성에 액세스 할 수 없습니다'라고 말하면 나쁜 액세스를 얻은 속성에 액세스하려고 시도하거나 단순히 nil을 반환하는 것을 의미합니까? 현재 초기화 메서드를 살펴보면 현재 속성에 액세스하지 않고 인스턴스 변수 만 볼 수 있기 때문에 사용자가 말하는 내용이 맞는지 확실하지 않습니다.

인스턴스 변수와 속성이 섞여있을 수 있습니다. 오브젝티브 C의 속성은 다음과 같습니다

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

... 앱 구조는 나에게 약간의 혼란이다. 초기화 메소드는 클래스, 메소드가 아닌 인스턴스이므로 ImageViewer을 하나만 작성하면 다른 ImageViewer을 작성할 수 있습니다. initWithFrame으로 전화하는 진정한 초기화 방법을 시도해 보시기 바랍니다. 애플의 구조가 문제를 디버그하는 데 도움이되지 않는다고 생각할 때, 애플의 소개 Objective-C 문서를 살펴 보는 것이 도움이된다고 생각한다.

+0

UIView에 대한 설명서를 읽었습니다. 나는 당신이 클래스와 인스턴스 사이의 혼란을 의미한다는 것을 정확히 알 수 있습니다. 나는 initWithFrame이 UIView의 초기화 메소드 였는지 모르고있었습니다. 기본적으로로드 이미지의 코드를 initWithFrame 메서드의 뷰에 복사했습니다. 이제 몇 가지 질문이 있습니다. 설명서에서 initWithFrame 메서드를 사용자 정의하거나 overwid를 사용하여 ow 매개 변수를 initWithFrame 메서드에 추가 할 수 있다고 말했습니까? 또한 addSubview와 함께 내보기 컨트롤러에서보기를 추가 할 때 initWithFrame 메소드가 호출됩니까? @lxt – ScottOBot