1

I have a button action function in a controller named as "Remotecontroller". this is the method:

-(IBAction)startDiscover:(id)sender{.....}

I have another controller named as "iptvViewcontroller". I need to call the above method in this "iptvViewcontroller" when a button click. this is my button click function:

btnRefresh3 = [UIButton buttonWithType:UIButtonTypeCustom];
            btnRefresh3.frame = CGRectMake(0, 0, 25, 20);
            [btnRefresh3 addTarget:self action:@selector(startDiscover:) 
            [arrLeftBarItems addObject:barButtonItem3];

Please somebody help me to make it work.

Doro
  • 11
  • 3
  • You can easily do this if you can find the reference of `RemoteController` if it exists in memory and assign instead of `self in addTarget`. – iphonic Oct 09 '15 at 06:04

3 Answers3

1

By creating and using custom delegate , you can achieve that functionality.

An Objective-C delegate is an object that has been assigned to the delegate property another object.

See below links for details and How to create and use custom delegate.

1.How do I create delegates in Objective-C?

2.How to use custom delegates in Objective-C

Community
  • 1
  • 1
Hardik Shekhat
  • 1,680
  • 12
  • 21
0

In Remotecontroller.m, set NotificationCenter like this in your viewDidLoad :

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

In Remotecontroller.m

-(void)startDiscover
{
    /////Your Code
}

In iptvViewcontroller.m

[btnRefresh3 addTarget:self action:@selector(startDiscoverFromRemoteController);


-(void)startDiscoverFromRemoteController
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"discover" object:nil];
}
Rumin
  • 3,787
  • 3
  • 27
  • 30
0

To call one method to another class you have to write your method in Remotecontroller.h file

-(IBAction)startDiscover:(id)sender;

then your remotecontroller class import in iptvViewcontroller class in .h file like this

#import "Remotecontroller.h"

now to use that method you have to create and alloc init object of remotecontroller class in iptvViewcontroller class in iptvViewcontroller.h file viewdidload() method

Remotecontroller *remote;

in iptvViewcontroller.m file

remote = [Remotecontroller alloc]init];

now you can use your method throughout the class

[remote startDiscover:parameter];
Ashish Thakkar
  • 944
  • 8
  • 27