ios – iPhone – 从另一个视图控制器调用函数

ios – iPhone – 从另一个视图控制器调用函数,第1张

概述我有一个名为sendDataToMotor的函数.它在我的First View Controller类中.我有另一个名为SecondViewController的视图控制器.我需要从Second View Controller.m类调用此函数.我试着宣布这个属性: @property(nonatomic,assign)UIViewController* firstController; 在我的Se 我有一个名为sendDataToMotor的函数.它在我的First VIEw Controller类中.我有另一个名为SecondVIEwController的视图控制器.我需要从Second VIEw Controller.m类调用此函数.我试着宣布这个属性:

@property(nonatomic,assign)UIVIEwController* firstController;

在我的SecondVIEwController.h类中.此外,我在我的SecondVIEwController.m类的vIEwDIDLoad部分编写了代码(我希望调用该函数).

secondVIEwController = [[SecondVIEwController alloc] initWithNibname:@"secondVIEwController" bundle:nil];secondVIEwController.firstController = self;[self.firstController performSelector:@selector(sendDataToMotor)];

但是,由于未声明的标识符问题,我在该代码(secondVIEwController)中的第一个单词出错.此外,我在第二行(secondVIEwController.firstController = self)中出错,因为secondVIEwController具有未知的名称类型.

总而言之,我不在乎你是否使用上面的代码来回答我的问题:这只是我试图在网上找到的东西.但是,我正在寻找从另一个VIEw Controller调用函数的最简单方法.

解决方法 通知中心可以解决您的问题.

接收器UIVIEwController

- (voID)vIEwDIDLoad {    [[NSNotificationCenter defaultCenter] addobserver:self        selector:@selector(receiveNotification:)         name:@"myNotification"        object:nil];}- (voID)receiveNotification:(NSNotification *)notification{    if ([[notification name] isEqualToString:@"myNotification"]) {       //doSomething here.    }}

发件人UIVIEwController

- (voID)sendNotification {    [[NSNotificationCenter defaultCenter] postNotificationname:@"myNotification" object:self];}
总结

以上是内存溢出为你收集整理的ios – iPhone – 从另一个视图控制器调用函数全部内容,希望文章能够帮你解决ios – iPhone – 从另一个视图控制器调用函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1012288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存