views:

1373

answers:

2

Does anyone know how to update a property value from the subview (child) view controller? I have a int property called statusid defined with gettor/settor in parent view controller. [self.view addSubview:detailsVC.view];

In the child subview, I trying calling [super statusid:updatedValue]; to update statusid to a new value, but this creates an error. How can i update statusid in the parent? Anyone know how to do this?

A: 

Calling a method on super calls the superclass's implementation of a method, it does call the superview's/super view controller's implementation.

You either need to keep a reference to the parent from the child view controller and call the setStatusId: method on the parent, or create a delegate pattern between the two that will let the child's delegate (likely set to the parent) know that the status ID changed.

Martin Gordon
Do you have an code example to: keep a reference to the parent from the child view controller and call the setStatusId: method on the parent? Also, which method is more elegant the delegate pattern or the setStatusId method on parent?
Zap
+3  A: 

with "super" you access your base class, the one your current class has inherited from

to do what you've explained, you need to access a property of your parent view, which is rather complicated since this will most likely end with both classes trying to reference each other. thus you will most likely have to create a delegate pattern, looking somewhat like this

ParentView.h

@protocol IAmYourFatherAndMotherProtocol

@class ChildView;

@interface ParentView : UIViewController <IAmYourFatherAndMotherProtocol>
{
NSInteger statusID;
}

@property (nonatomic) NSInteger statusID;

@protocol IAmYourFatherAndMotherProtocol
@property (nonatomic) NSInteger statusID;
@end

@end

in ChildView.h

#import "ParentView.h"

@interface ChildView : UIViewController
{
  id<IAmYourFatherAndMotherProtocol> delegate;
}

@property (nonatomic, assign) id <IAmYourFatherAndMotherProtocol> delegate;

when creating your ChildView in ParentView.m, you have to set "self" as delegate, eg:

ChildView *newChild = [[ChildView alloc] init];
newChild.delegate = self;

by doing so, you can access "statusID" of your ParentView in ChildView.m like this:

delegate.statusID = 1337;

hope this helps

gabtub
+1 for the protocol name
slf
Just a clarification. What I am trying to do is change a property in a MKAnnotation view which contains a AnnotationView which has a callout to a UIViewController. I want to change a property value of the MKAnnotation from the callout's viewcontroller.
Zap
So you have a MKAnnotationView which contains a(nother?) (MK?)AnnotationView?Anyhow, i'm pretty sure that you will still have to do it in way similar to what i've posted.
gabtub
Is there any way to expand this to a method? In other words, call something like [delegate doSomething:YES];???
Brett
You would need to declare that specific method in the protocol, which would in this case be the IAmYourFatherAndMotherProtocol (should've picked a shorter name..) and implement it in the delegate (ParentView), much like shown here with the NSInteger
gabtub