use*_*450 3 objective-c uiviewcontroller ios
我有一个名为sendDataToMotor的函数.它在我的First View Controller类中.我有另一个名为SecondViewController的视图控制器.我需要从Second View Controller.m类调用此函数.我试着宣布这个属性:
@property(nonatomic,assign)UIViewController* firstController;
Run Code Online (Sandbox Code Playgroud)
在我的SecondViewController.h类中.此外,我在我的SecondViewController.m类的viewDidLoad部分编写了代码(我希望调用该函数).
secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
secondViewController.firstController = self;
[self.firstController performSelector:@selector(sendDataToMotor)];
Run Code Online (Sandbox Code Playgroud)
但是,由于未声明的标识符问题,我在该代码(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.
}
}
Run Code Online (Sandbox Code Playgroud)
发件人UIViewController
- (void)sendNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4764 次 |
| 最近记录: |