Reputation: 849
I have a view that is loaded as a subview programmatically. The view has a three properties defined in the .h and synthesized in the .m. The view is loaded by creating an object of the view in the parent controller and then calling it's initialization function. In the initialization function I set a number of values to the their respective properties. For some reason I am not able to access the properties outside of the initialization function. My first thought was that the properties were defined incorrectly but after messing around with their strong and weak attributes nothing changed.
The UIView's .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
The relevant functions in the 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;
}
The above code allows you to access the values assigned to _mainImage, _parentView and _imageId. But when I try to access them through a private function defined in the .m the return the initialized value as can be seen below.
- (void)uploadPhoto:(id)sender
{
//These all return as empty or uninitialized
NSLog(@"%@", _mainImage);
NSLog(@"%d", _imageId);
}
Why is this? Am I defining the properties incorrectly in the .h or is it because self doesn't refer to the instance of ImageView defined in loadImageIntoViewer? How can I fix this?
Upvotes: 1
Views: 252
Reputation: 31294
Some thoughts:
Firstly, you don't need to declare the @synthesize
statements anymore, assuming you're working on a recent version of XCode. The compiler will automatically insert the synthesize statements for you, and create instance variables appended with a underscore (as you've done).
Secondly, when you say you are 'not able to access the properties outside of the initialization function' - do you mean when you try to access the properties you get a bad access, or they simply return nil? I'm not sure if you mean what you say, because looking at your current initialization method you don't currently access the properties at all, only their instance variables.
I think you might be getting instance variables and properties mixed up. A property in Objective-C looks like this:
NSLog(@"%@", self.mainImage);
...your app structure is also a little confusing to me. Your initialisation method is an instance, rather than class, method, so you create one ImageViewer
only to then have it create another ImageViewer
. I'd suggest you try and create a true init method, one that calls out to initWithFrame
. I think maybe you might find it helpful to look through Apple's intro Objective-C documentation, as I don't think the structure of your app is helping you debug the issue.
Upvotes: 1