简体   繁体   English

从一个类调用函数到另一类

[英]Call the function from one class to another class

I have two view controllers: one ViewController and the other TextViewController . 我有两个视图控制器:一个ViewController和另一个TextViewController I give the definition of the function in ViewController and want to call this in TextViewController, but I am unable to do this. 我在ViewController中给出了函数的定义,并想在TextViewController中调用它,但是我无法做到这一点。 Please help me through this. 请帮助我。

Declaration of the function in .h class .h类中的函数声明

@interface ViewController : UIViewController

-(void)getVideo;

Definition of the function in .m class .m类中函数的定义

-(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 我在TextViewController.m中这样称呼它

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

Any help will be appreciated. 任何帮助将不胜感激。

Use the NSNotificationCenter PostNotificationName method and example is given below, 使用NSNotificationCenter PostNotificationName方法,下面给出示例,

First you create the add observer notification in your viewDidLoad:, 首先,您在viewDidLoad中创建添加观察者通知:

ViewController.m class: ViewController.m类:

- (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, 然后在以下给出的调用函数形式中设置postNotification名称,

First Class(calling class): 头等舱(上课):

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

dont forget your notification name is same name enter in PostNotificationName and addObserver name. 不要忘记您的通知名称与PostNotificationName和addObserver名称相同。

hope its helpful 希望它有所帮助

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. 委托协议和实现的更改将由编译器获取,如果不更改(如果使用选择器,则为EG),则您的应用程序至少会在开发过程中崩溃,而不仅仅是事情无法正常运行。 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. 对于所有其他事件,我将始终尝试使用委托模式,如果由于某种原因我不能这样做,则我将首先评估我的应用程序结构是否存在严重错误,如果没有,我将仅使用通知。

Do this: 做这个:

ViewController.m ViewController.m

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

TextViewController.m 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM