Pass property from one controller to another on iOS

I am working with Azure Mobile Service on iOS and I have enabled authentication. I have extended the default application that Microsoft provides as an example for using tabBarController. One default controller is used by MS and is called ToDoListController.

I want to use the todoService property that is used by the ToDoListController in the second controller which is AddItemController. So, in the header of ToDoListController I have

@property (strong, nonatomic) TodoService *todoService;

      

and in m of the same controller i have its synthesis.

When I want to use this property in the tabBarController I call it

(((TodoListController *)self.parentViewController).todoService)

      

but i get

[UITabBarController todoService]: unrecognized selector posted to instance

and

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController todoService]: unrecognized selector sent to instance 

      

+3


source to share


5 answers


It looks like you are trying to access one of your tab bar controllers. try it



for (UIViewController *v in ((UITabBarController*)self.parentviewController).viewControllers)
{
     UIViewController *vc = v;

     if ([v isKindOfClass:[TodoListController class])
     {
         ((TodoListController *)v).todoservice;
     }
}

      

+4


source


Have you imported the ToDoService .h file in your tabBarController? This will show the visibility for the tabBarController. Apologies in advance for not using the comment function, I am not allowed.



0


source


you are sending the selector to the wrong controller, try to determine which one in your hierarchy exactly matches your TodoListController. Why do you think parentViewController is TodoListController?

if you are presenting a modal current VC inside a UITabBarController then parentVC is a UITabBarController.

also you can try self.parentViewController.parentViewController

;

0


source


I would suggest making your TodoService a single. There are many ways to do this, here is a simple piece of code that creates singleton access, you can access anywhere *. Add this method signature to your TodoService.h file

+ (TodoService *) defaultService;

      

And this implementation

TodoService *todoService;

+ (TodoService *)defaultService
{
    if (todoService == nil) {
        todoService = [[TodoService alloc] init];
    }
    return todoService;
}

      

Now from anywhere in the application you can access one instance via

TodoService* service = [TodoService defaultService];
[service doSomething];

      

* Note that there are better ways to implement a singleton in Objective C, but in most cases this is a great method where you only call defaultService on the main thread.

0


source


Finally, the problem was a breakpoint. The data transfer went through without problems

(((TodoListController *)self.parentViewController).todoService)

      

0


source







All Articles