0

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.

martinez314
  • 12,162
  • 5
  • 36
  • 63
Neha Purwar
  • 175
  • 1
  • 13
  • User Custom Delegate (Protocol) or NSNotificationCenter – Nitin Gohel Jun 02 '16 at 10:35
  • 2
    possibly duplicate of http://stackoverflow.com/questions/9731126/how-to-call-method-from-one-class-in-another-ios – Kuldeep Singh Jun 02 '16 at 10:35
  • add -(void)getVideo; it in ViewController.h file – Mohammad Sadiq Shaikh Jun 02 '16 at 10:36
  • Yes @MohammadSadiqShaikh – Neha Purwar Jun 02 '16 at 10:37
  • by looking at the code it seems you need to write code in TextViewController itself. You have added MPMoviePlayerViewController as subview in getVideo function – Mohammad Sadiq Shaikh Jun 02 '16 at 10:38
  • Can you give me an example of NSNotificationCenter @NitinGohel – Neha Purwar Jun 02 '16 at 10:38
  • Actually i want to call this function in TextViewController And want to define it in viewcontroller class @MohammadSadiqShaikh – Neha Purwar Jun 02 '16 at 10:41
  • video will not be get added to TextViewController. It will be added to ViewController and you will not see it - i'm assuming TextViewController is on the top – Mohammad Sadiq Shaikh Jun 02 '16 at 10:47
  • What you exactly want to achieve with above code ? In "getVideo" method you are adding a video to "View-controller's" view. And now you are creating a object of it and you are expecting it to show in second view-controller by just calling that method. This is not the correct way. – Wolverine Jun 02 '16 at 11:11
  • Is it possible? if not . Please suggest me any idea to do this @Wolverine – Neha Purwar Jun 02 '16 at 11:13
  • In "getVideo" function, you are loading a video file from you app bundle. Then in your "TextViewController", Write this method and call it. It will add your video to "TextViewController's" view. – Wolverine Jun 02 '16 at 11:18
  • And if you want to use "ViewController", Then make a method which will return a URL of that video and create your player in "TextViewController" and add it as subview. – Wolverine Jun 02 '16 at 11:20
  • Ok , Can you tell me how can add the editable text in this video in it's pause state? @Wolverine – Neha Purwar Jun 02 '16 at 11:21
  • @NehaPurwar Just add `-(void)getVideo;` in `ViewController.h` file and your method will be called **on the new instance of VC that you created**. If you want to call the method on the original instance of ViewController, use Notifications or delegate pattern as suggested by others as well – NSNoob Jun 02 '16 at 11:29
  • You may need to create overlay-view on video view. See the view hierarchy documentations of apple to understand in more detail. – Wolverine Jun 02 '16 at 11:37
  • u need delegate ? – Jagveer Singh Sep 07 '16 at 11:07

3 Answers3

1

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

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
  • can you explain what's the mean of postnotification and according to my requirement which name will be posted here. and where this line will be added [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationName" object:nil]; @lyyappan Ravi – Neha Purwar Jun 02 '16 at 11:00
  • NSNotification is used for viewcontrollerB have function, but viewcontrollerA call the viewcontrollerB function, thats the time used NSNotificaitonCenter or custom delegate methods, its call one viewcontroller function call another viewcontroller method call. – Iyyappan Ravi Jun 02 '16 at 11:56
0

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.

NSPratik
  • 4,714
  • 7
  • 51
  • 81
0

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];
}
NSPratik
  • 4,714
  • 7
  • 51
  • 81