2013-05-09 103 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相關職能.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實例?我怎樣才能解決這個問題?

+0

一個問題,如果你宣佈他們強大而不是複製它的私人方法工作? – Radu 2013-05-09 14:16:33

+0

我相信你不想要UIViewController的副本 - 你想要一個弱引用。 – 2013-05-09 14:21:38

回答

1

的幾點思考:

首先,你不需要再申報@synthesize語句,假設你在最近版本的Xcode工作。編譯器會自動爲你插入合成語句,並創建一個帶有下劃線的實例變量(如你所做的那樣)。其次,當你說'你不能訪問初始化函數之外的屬性' - 你的意思是當你試圖訪問屬性時,你會得到一個不好的訪問,或者他們只是返回nil?我不確定你說的是什麼意思,因爲看看你當前的初始化方法,你當前沒有訪問屬性,只有它們的實例變量。

我想你可能會得到實例變量和混合的屬性。在Objective-C的屬性看起來是這樣的:

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

...您的應用程序結構也有點迷惑了我。您的初始化方法是一個實例,而不是類,方法,因此您只創建一個ImageViewer,然後再創建另一個ImageViewer。我建議你嘗試創建一個真正的init方法,其中一個調用initWithFrame。我想也許你可能會發現瀏覽Apple的介紹Objective-C文檔很有幫助,因爲我認爲你的應用程序的結構不能幫助你調試問題。

+0

我讀了UIView的文檔。我明白你在課堂和實例之間的混淆意味着什麼。我不知道initWithFrame是UIView的初始化方法。我基本上已經將代碼從加載映像複製到initWithFrame方法的視圖中。現在我有幾個問題,文檔說initWithFrame方法可以定製或overdid我可以將我的ow參數添加到initWithFrame方法嗎?當我使用addSubview從主視圖控制器添加視圖時,initWithFrame方法也會被調用嗎? @lxt – ScottOBot 2013-05-09 18:31:38