从应用程序委托调用 UIViewController 方法 [英] Calling UIViewController method from app delegate

查看:30
本文介绍了从应用程序委托调用 UIViewController 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题有重复,但我的情况不同.

I know there are duplicates of this question but my situation is different here.

当用户回到家时 (void)applicationDidEnterBackgroundAppDelegate 类被调用.然而,一旦用户按下主页按钮,我不希望用户再次看到这个视图控制器,所以我有一个名为 (void)goToBeginning 的方法可以切换到另一个视图控制器.我希望能够从 AppDelegate 调用此方法.我真的不想为此使用 NotificationCenter.还有这里选择的解决方案:从应用委托调用视图控制器方法对我不起作用,因为它初始化了新对象,而我希望能够调用视图中已经存在的对象.我怎样才能做到这一点?我使用的是 iOS 7 和 XCode 5.

When user goes back to the home (void)applicationDidEnterBackground gets invoked from the AppDelegate class. However once user presses home button, I don't want user to see this view controller again, so I have a method named (void)goToBeginning that switches to another view controller. I want to be able to call this method from AppDelegate. I don't really want to use NotificationCenter for this. Also the picked solution here: Calling view controller method from app delegate does not work for me as it initialises new object whereas I want to be able to call an object that is already in the view. How can I do that? I am using iOS 7 and XCode 5.

推荐答案

  1. 通知.但你不想要这个.
  2. 您可以在 AppDelegate 中获取对该 viewController 的引用.比在 (void)applicationDidEnterBackground
  3. 中调用 (void)goToBeginning 方法
  1. Notification. But you don't want this.
  2. You can get the reference to your that viewController in the AppDelegate. Than call that (void)goToBeginning method in the (void)applicationDidEnterBackground

例如:在你的 ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.myViewController = self;
}

在你的 AppDelegate 中:

@class MyViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (weak, nonatomic) MyViewController *myViewController;

@end

AppDelegate的实现中:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.myViewController goToBeginning];
}

这篇关于从应用程序委托调用 UIViewController 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆