问题描述
我知道这个问题有重复,但我的情况不同.
I know there are duplicates of this question but my situation is different here.
当用户回到家时 (void)applicationDidEnterBackground
从 AppDelegate
类被调用.然而,一旦用户按下主页按钮,我不希望用户再次看到这个视图控制器,所以我有一个名为 (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 delegatedoes 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.
推荐答案
- 通知.但你不想要这个.
- 您可以在
AppDelegate
中获取对该viewController
的引用.比在(void)applicationDidEnterBackground
中调用
(void)goToBeginning
方法- Notification. But you don't want this.
- You can get the reference to your that
viewController
in theAppDelegate
. 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 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!