![](/img/trans.png)
[英]IOS Using method from ViewController.m in AppDelegate.m?
[英]How to call function in ViewController.m from AppDelegate?
- (void)applicationDidBecomeActive:(UIApplication *)application {
UIViewController* root = _window.rootViewController;
UINavigationController* navController = (UINavigationController*)root;
UIViewController mycontroller = (UIViewController )[[navController viewControllers] objectAtIndex:0];
[mycontroller serverSync];
}
我使用此代码,但出现错误:
ld:架构x86_64 clang的110个重复符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)
怎么修?
110 duplicate symbols
表示,与尝试从应用程序委托中调用视图控制器的serverSync
函数相比,您遇到的问题要serverSync
。
与其在您的应用程序委托中进行serverSync
将其放入视图控制器的viewDidLoad
方法中。
更好的是,创建一个执行serverSync
的单例对象,并且视图控制器可以从那里访问和使用服务器数据。
您可以为此使用NSNotificationCenter。 这是例子。
在您的AppDelgate.m中
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"iOStpoint.wordpress.com"
object:self];
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
在您的ViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"iOStpoint.wordpress.com"
object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"iOStpoint.wordpress.com"])
NSLog (@"Successfully received the test notification!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.