从app delegate调用UIViewController方法 [英] Calling UIViewController method from app delegate
问题描述
我知道这个问题有重复,但我的情况有所不同。
I know there are duplicates of this question but my situation is different here.
当用户返回家中时(void)applicationDidEnterBackground
从 AppDelegate
类调用。但是一旦用户按下主页按钮,我不希望用户再次看到这个视图控制器,所以我有一个名为(void)goToBeginning
的方法切换到另一个视图控制器。我希望能够从AppDelegate调用此方法。我真的不想使用 NotificationCenter
。此处选择的解决方案:
从app delegate调用视图控制器方法
对我来说不起作用,因为它初始化了新对象,而我希望能够调用已经在视图中的对象。我怎样才能做到这一点?我使用的是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.
推荐答案
- 通知。但是你不想要这个。
- 你可以在
AppDelegate中获得对
。在viewController
的引用(void)applicationDidEnterBackground
中调用
(void)goToBeginning
方法例如:在你的 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];
}
这篇关于从app delegate调用UIViewController方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!