Antoine Andrieu
Antoine Andrieu

Reputation: 65

How to send data to the RootViewController

Here is my problem:

I have a basic application with a UIViewController embedded in a NavigationController. It is also the RootViewController of the application. From there I have a push segue to a normal UIViewControllerand a second push segue to a UITableViewController with its own UIViewController for the detailed view.

In the root view there is an instance of the class whose purpose is to send message with a define protocol. In the table view the user will select the type of message he wants to send and in the detail view the content of that particular type of message.

Now the user has specified everything and I want him to push a "send" button. That button must do two things: pop back to the root view and send the user defined message by the protocol class instance. I can do the pop back just fine with:

[self.navigationController popToRootViewControllerAnimated:true];

but I have no idea how to send the message (a class instance) back to the root view. The application is still fresh so I can completely change the structure if this one is not correct.

The best for me would be to access the protocol class instance from everywhere (I will need it in the other UIViewController) but I am not sure how to do that so that's why I thought of sending the message back to the root view.

If you know how to do one of the two above please give me a hand!

Cheers.

EDIT: Technically the NavigationController is the initial ViewController so I am not really sure who is the RootViewController anymore.

Upvotes: 3

Views: 2286

Answers (5)

KDeogharkar
KDeogharkar

Reputation: 10959

one way is to use protocol and other way is to pass your root view controller instance to your tableviewcontroller(via view controller) using your custom init method like:

UIViewController.m

- (id)initWithRoot:(id)rootInstance
      withNibNameOrNil:(NSString *)nibNameOrNil
                bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.root = (RootView *)rootInstance;
    }

}

RootViewController.m

viewcontrollerInstance = [[viewcontroller alloc] initWithRoot:self                      withNibNameOrNil:@"viewcontroller"
bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:viewcontrollerInstance animated:YES];

UITableViewController.m

- (id)initWithRoot:(id)rootInstance
      withNibNameOrNil:(NSString *)nibNameOrNil
                bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.root = (RootView *)rootInstance;
    }

}

ViewController.m

tableViewInstance = [[tablecontroller alloc] initWithRoot:self                      withNibNameOrNil:@"tablecontroller"
bundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:tableViewInstance animated:YES];

UITableViewController.m

now on table view controller use your root instance(come from view controller) to call the function of root view controller like:

[self.root displayMessage:message];

sorry for the typo. hope this will help.

Upvotes: 1

Pankaj Wadhwa
Pankaj Wadhwa

Reputation: 3063

You Can also send Notification whenever you move to root view controller

by adding observer to it.

Upvotes: 1

Reinhard Männer
Reinhard Männer

Reputation: 15217

You could try:

UIApplication *myApp = [UIApplication sharedApplication];
UIWindow *frontWindow = [myApp.windows lastObject];
UIViewController *myRootViewController = frontWindow.rootViewController;

Upvotes: 1

wasim
wasim

Reputation: 708

you can Create an application object and assign the message to it. and use it on your root view controller. if i understood your question correctly this might help.

Upvotes: 1

Thomas Keuleers
Thomas Keuleers

Reputation: 6115

First: You could do: (only works when your view has been added to a the window)

[self.view.window.rootviewcontroller doSomething];

Second option is to define a property on your appDelegate:

@property (nonatomic, strong) UIViewController *root;

And call it through:

AppDelegate *appDelegate= (YourAppDelegateClass *) [[UIApplication sharedApplication] delegate];

[appDelegate.root doSomething];

Upvotes: 4

Related Questions