iphone - IOS Issue Accessing Class Properties -
i have view loaded subview programmatically. view has 3 properties defined in .h , synthesized in .m. view loaded creating object of view in parent controller , calling it's initialization function. in initialization function set number of values respective properties. reason not able access properties outside of initialization function. first thought properties defined incorrectly after messing around strong , 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 uiviews .m
//implementation of 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 new view same frame size 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 components , functionalities program //when use _mainimage bellow works 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 access values assigned _mainimage, _parentview , _imageid. when try access them through private function defined in .m return initialized value can seen below.
- (void)uploadphoto:(id)sender { //these return empty or uninitialized nslog(@"%@", _mainimage); nslog(@"%d", _imageid); }
why this? defining properties incorrectly in .h or because self doesn't refer instance of imageview defined in loadimageintoviewer? how can fix this?
some thoughts:
firstly, don't need declare @synthesize
statements anymore, assuming you're working on recent version of xcode. compiler automatically insert synthesize statements you, , create instance variables appended underscore (as you've done).
secondly, when 'not able access properties outside of initialization function' - mean when try access properties bad access, or return nil? i'm not sure if mean say, because looking @ current initialization method don't access properties @ all, instance variables.
i think might getting instance variables , properties mixed up. property in objective-c looks this:
nslog(@"%@", self.mainimage);
...your app structure little confusing me. initialisation method instance, rather class, method, create 1 imageviewer
have create imageviewer
. i'd suggest try , create true init method, 1 calls out initwithframe
. think maybe might find helpful through apple's intro objective-c documentation, don't think structure of app helping debug issue.
Comments
Post a Comment