Neha Purwar
Neha Purwar

Reputation: 175

Call the function from one class to another class

I have two view controllers: one ViewController and the other TextViewController. I give the definition of the function in ViewController and want to call this in TextViewController, but I am unable to do this. Please help me through this.

Declaration of the function in .h class

@interface ViewController : UIViewController

-(void)getVideo;

Definition of the function in .m class

-(void)getVideo{
NSString *inputPath = [[NSBundle mainBundle]pathForResource:@"GalaxyTutorial" ofType:@"mp4"];
NSURL *inputURL = [[NSURL alloc]initFileURLWithPath:inputPath isDirectory:YES];
MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: inputURL];
[mpc.view setFrame: self.view.bounds];
[self.view addSubview:moviePlayer.view];
}

And i am calling this in TextViewController.m like this

ViewController *dtl = [[ViewController alloc]init];
[dtl getVideo];

Any help will be appreciated.

Upvotes: 0

Views: 229

Answers (3)

NSPratik
NSPratik

Reputation: 4846

Do this:

ViewController.m

-(NSURL *)getVideoURL
{
    NSString *inputPath = [[NSBundle mainBundle]pathForResource:@"GalaxyTutorial" ofType:@"mp4"];
    NSURL *inputURL = [[NSURL alloc]initFileURLWithPath:inputPath isDirectory:YES];
    return inputURL;
}

TextViewController.m

-(void)playVideo
{
    ViewController *dtl = [[ViewController alloc]init];
    NSURL *url = [dtl getVideo];

    MPMoviePlayerViewController* moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL: inputURL];
    [mpc.view setFrame: self.view.bounds];
    [self.view addSubview:moviePlayer.view];
}

Upvotes: 0

NSPratik
NSPratik

Reputation: 4846

I suggest you to use the delegate pattern rather than a notification..

I believe that communication between controllers should be made very clear through the use of well named protocols and well named protocol method definitions. Making the effort to define these protocol methods will yield much easier code to read, and provide much more traceability within your app. Changes to delegate protocols and implementations will be picked up by the compiler, and if not (EG if you are using selectors) your app will at least crash during development, rather than just having things not working properly. Even in scenarios where multiple controllers need to be informed of the same event, as long as your application is well structured in a controller hierarchy, messages can be passed up the hierarchy where they can be passed back down to all controllers that need to know of the events.

Of course there are exceptions where the delegation pattern just does not fit and notifications make more sense. An example might be an event that every controller in your application needs to know of. However these types of scenarios are very rare. Another example might be in scenarios whereby you are building a framework that needs to announce events to the application it is running in.

As a rule of thumb I will only use observation, for property level events within objects that I did not code, or for property level events within model objects that are tightly bound to a view object. For all other events, I will always try to use a delegate pattern, if for some reason I can’t do that, I will first assess whether I have something critically wrong with my app’s structure, and if not, only then will I use notifications.

Upvotes: 0

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

Use the NSNotificationCenter PostNotificationName method and example is given below,

First you create the add observer notification in your viewDidLoad:,

ViewController.m class:

- (void)viewDidLoad {
    [super viewDidLoad];
    // put your code

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callService:) name:@"NotificationName" object:nil];

}

- (void)callService:(NSNotification *)noti {
    [self getVideo];
}

then set the postNotification name in called functions give below format,

First Class(calling class):

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:nil];

dont forget your notification name is same name enter in PostNotificationName and addObserver name.

hope its helpful

Upvotes: 1

Related Questions