从app delegate调用UIViewController方法

Sar*_*aya 12 objective-c uiviewcontroller ios appdelegate

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

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

sun*_*ppy 42

  1. 通知.但你不想要这个.
  2. 你可以得到关于你说viewControllerAppDelegate.比调用那个(void)goToBeginning方法(void)applicationDidEnterBackground

例如:在你的 ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.myViewController = self;
}
Run Code Online (Sandbox Code Playgroud)

在你的AppDelegate:

@class MyViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (weak, nonatomic) MyViewController *myViewController;

@end
Run Code Online (Sandbox Code Playgroud)

并在AppDelegate实施中:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [self.myViewController goToBeginning];
}
Run Code Online (Sandbox Code Playgroud)