0
votes

RootView has a label, and a method which updates this label. There is also a number of views located inside container views. Any of these should be able to update this label. They do so by calling this method. I am able to call it from these subview-controllers, but it does not update the label. Calling it from the root view does update the label. I've read about delegates, but I haven't been successful in adding it. Is that the way I should do it, by delegates?

this is from the subView. Please correct me If i am wrong, but I suppose it doesn't work because it creates a new rootView, and not using the old one.

Storyboard image here: http://i58.tinypic.com/2e17j1e.jpg

[super viewDidLoad];
self.rootView = [[RootViewController alloc]init];
[self.rootView displaymessage:@"Hello sent from child"];
2
So, you are using a NavigationController. Two, in fact. This makes it complex.ZeMoon
The navigationController that you present from the MainView, is it a push or a modal segue?ZeMoon
Should I remove the navigation controllers? The segues are a custom segue, no animation at all. Did use push segues earlier, just didn't want the animation.Runar
I would suggest you bring it back to push segue, and set the animated property as NO.ZeMoon
I am posting an edit in a couple of minutesZeMoon

2 Answers

0
votes

What you are doing is wrong. Calling self.rootView = [[RootViewController alloc]init]; actually creates a new instance of RootViewController. Fortunaltely, UIVIewController holds a property called presentingViewController which represents the viewController which presented the child.

self.rootView = self.presentingViewController;
[self.rootView displaymessage:@"Hello sent from child"];

Or simply,

[(RootViewController*)self.presentingViewController displaymessage:@"Hello sent from child"];

Here's the link to Apple's documentation regarding the presentingViewController property.

EDIT:

Looking at the complexity, of your storyboard, I would suggest you use NSNotificationCenter to change the label. Here's how:

In RootViewController's viewDidLoad, paste this code:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displaymessageFromNotification:) name:@"rootDisplayMessage" object:nil];

Then add the following method to RootViewController's .m file:

-(void)displaymessageFromNotification:(NSNotification*)notification
{
    NSDictionary *data = [notification userInfo];
    NSString *message = [data objectForKey:@"message"];

    [self displaymessage:message];
}

Then, from anywhere in the code where you want to display a message in the RootViewController, use these lines:

NSDictionary *dicMessage  = [NSDictionary dictionaryWithObject:@"Your message here." forKey:@"message"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"rootDisplayMessage" object:nil userInfo:dicMessage];
0
votes

try some code like this:

//rootView add child controllers place

ChildControllers *childVc = [[ChildControllers alloc] init];
childVc.rootView = theRootViewAddChildVc; 

//child controller.h

@property (nonatomic, weak) RootViewController * rootView;

//child controller.m update label place:

[self.rootView updateLabel];