hanumanDev
hanumanDev

Reputation: 6614

How do I call a method from another view controller

I want to call this method:

- (void)getUserFriendTargetDialogRequest {
    currentAPICall = kAPIFriendsForTargetDialogRequests;
    [self apiGraphFriends];
}

from this conditional statement that's in another viewcontroller:

if (idx == 2) {
        NSLog(@"you touched menu 2");

        APICallsViewController *apiViewController = [APICallsViewController alloc];
        [self.navigationController pushViewController:apiViewController animated:YES]; 
    }

can anyone help with the syntax on this?

thanks so much

Upvotes: 3

Views: 13731

Answers (3)

Abhishek Singh
Abhishek Singh

Reputation: 6166

1>Just alloc the class in which the method is and then call it through object of that class.

ViewControllWithMethod *view=[ViewControllWithMethod  alloc]]init];
[view getUserFriendTargetDialogRequest];

2>Instead of instance method you can make it as class method then you will be able to call it through className.getUserFriendTargetDialogRequest

Upvotes: 4

Matt
Matt

Reputation: 1586

First, you probably also want to init your APICallsViewController via:

    APICallsViewController *apiViewController = [[APICallsViewController alloc] initWithNibName:nil bundle:nil];

Then if that getUserFriend... is a method of APIViewController, you can do this:

    [apiViewController getUserFriendTargetDialogRequest];

However, since you aren't passing any arguments in from your other view controller, you might consider calling it in the init method, or the viewDidLoad method of APICallsViewController.

Upvotes: 12

OtherViewController *viewController = [[OtherViewController alloc]         
                                           init];
[viewController methodName];
[viewController release];

Upvotes: 5

Related Questions