Eric
Eric

Reputation: 2091

Set value of property in main view controller from dynamically instantiated class (objective-c)

I am totally out of ideas on this... I've tried so many variations that I am dizzy...

I have a main UIViewController which, at the touch of a button, adds another UIViewController to one of its subviews. When the dynamic UIVC gets added, a property in the main UIVC is updated to hold a reference to it (called currentObject). This is working fine.

The problem I am having is that if I add another dynamic UIVC, the property holding the reference is initially updated correctly, but no matter what I try, I can't get the property to update when I touch the first dynamic UIVC. Everything I try to set "currentObject" from a dynamic UIVC gives me an "unrecognized selector sent to class" error and then bails.

I'm holding off from putting code into this post at first. Not sure what I would post that would be helpful.

Thanks in advance!

Updated:

in DynamicModuleViewController.h:

@interface DynamicModuleViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate, UIGestureRecognizerDelegate, UITextViewDelegate, UIApplicationDelegate, MFMailComposeViewControllerDelegate>{

DynamicModule *currentObject;

}

@property(nonatomic, retain) DynamicModule *currentObject;

in DynamicModuleViewController.m:

@implementation DynamicModuleViewController

@synthesize currentObject;

-(void)addObject:(id)sender
{
DynamicModule *dm = [[DynamicModule alloc]init];
// positioning and PanGesture recognition code to allow dragging of module
currentObject = dm;
[mainView addSubview:currentObject.view];
}

@end

when added this way, from a button tap, it works fine.

Once more DynamicModules are instantiated, I need to update currentObject with the DynamicModule that was tapped last.

Upvotes: 1

Views: 1568

Answers (2)

S.C.
S.C.

Reputation: 2874

What about using the delegate pattern?

Upvotes: 0

Jonah
Jonah

Reputation: 17958

adds another UIViewController to one of its subviews

First of all this is a red flag to me. Adding a UIViewController's view as a subview is almost always the wrong way to manage a view hierarchy and a common mistake in iOS apps. Please see http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/ so I can stay on topic and avoid repeating myself.

currentObject = dm;

This sets the ivar backing your currentObject property directly. You are leaking the previous value of currentObject. You don't appear to be removing the previous currentObject's view from mainView. I suspect you are setting the currentObject to an autoreleased object, failing to retain it because you bypassed your setter, and eventually try to send a message to the released object resulting in either an "unrecognized selector" error as your message reaches whatever other object now occupies that memory address or a BAD_ACCESS error as you try to reference an object which no longer exists.

self.currentObject = foo is equivalent to [self setCurrentObject:foo] and is probably what you intended.

Upvotes: 1

Related Questions