Ios Xcode 5,当用户离开应用程序时,如何调用方法?
您好,我没有太多编程经验,我正在尝试在用户离开应用程序时调用一个方法。我知道我将使用app delegate方法Ios Xcode 5,当用户离开应用程序时,如何调用方法?,ios,objective-c,appdelegate,Ios,Objective C,Appdelegate,您好,我没有太多编程经验,我正在尝试在用户离开应用程序时调用一个方法。我知道我将使用app delegate方法applicationidenterbackground,但是如何使该调用成为我的ViewController类中的方法呢 非常感谢你的帮助 ViewController.h: #import <UIKit/UIKit.h> @interface ViewController : UIViewController <ADBannerViewDelegate> {
applicationidenterbackground
,但是如何使该调用成为我的ViewController
类中的方法呢
非常感谢你的帮助
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <ADBannerViewDelegate>
{
//Images and buttons
}
-(void)stop;
@end
#导入
@界面ViewController:UIViewController
{
//图像和按钮
}
-(无效)停止;
@结束
我不明白在UIViewController中调用方法的目的。尽管您可以在ApplicationIdentinterBackground
中创建viewcontroller的实例,并使用该对象调用viewcontroller中的相应方法,但我没有达到在UIViewController中调用该方法的目的。即使您可以在ApplicationIdentinterBackground
中创建viewcontroller的实例,并使用该对象调用viewcontroller中的相应方法,您也可以
ApplicationIdentinterBackground:
方法中调用ViewController方法。应用程序代理应该有一个指向根ViewController的指针,即:您的ViewControllerApplicationIdentinterBackground:
方法中调用ViewController方法。应用程序代理应该有一个指向根ViewController的指针,即:您的ViewController当您关闭或离开应用程序时,自动调用AppDelegate方法,您不需要在某些特定的ViewController中调用它们 要在应用程序处于后台时执行某些操作,您可以在AppDelegate的
ApplicationIdentinterBackground:
方法中实现您的逻辑
或者,如果您的应用程序未运行(表示已关闭)AppDelegate方法
didFinishLaunchingWithOptions:将被调用。当您关闭或离开应用程序时,AppDelegate方法将自动调用,您无需在特定的ViewController中调用它们
要在应用程序处于后台时执行某些操作,您可以在AppDelegate的ApplicationIdentinterBackground:
方法中实现您的逻辑
或者,如果您的应用程序未运行(表示已关闭)AppDelegate方法didfishlaunchwithoptions:将被调用。您可以在此处使用通知。在viewController的viewDidLoad中为此通知创建一个侦听器,并将函数分配给它。
例如:
在yourView控制器中,在viewDidLoad中添加以下内容
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEnabled) name:UIApplicationDidEnterBackgroundNotification object:nil];
您可以在此处使用通知。在viewController的viewDidLoad中为此通知创建一个侦听器,并将函数分配给它。
例如:
在yourView控制器中,在viewDidLoad中添加以下内容
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEnabled) name:UIApplicationDidEnterBackgroundNotification object:nil];
您可以通过创建该视图的实例,然后调用该视图的特定方法来实现这一点
否则,如果您的视图被打开,那么您可以通过以下代码找到它&然后在applicationidentinterbackground
NSArray *AllViewControllers = [self.navigationController viewControllers];
for (UIViewController *aViewController in AllViewControllers) {
//NSLog(@" >> Nav Stack %@", [aViewController class]);
if ([aViewController isKindOfClass:[YourViewController class]]) {
[(YourViewController *)aViewController yourMethodToCall];
break;
}
}
您可以通过创建该视图的实例,然后调用该视图的特定方法来实现这一点
否则,如果您的视图被打开,那么您可以通过以下代码找到它&然后在applicationidentinterbackground
NSArray *AllViewControllers = [self.navigationController viewControllers];
for (UIViewController *aViewController in AllViewControllers) {
//NSLog(@" >> Nav Stack %@", [aViewController class]);
if ([aViewController isKindOfClass:[YourViewController class]]) {
[(YourViewController *)aViewController yourMethodToCall];
break;
}
}
好的,那么在方法applicationIdentinterBackground中有什么代码?我试过[我的方法];它给了我一个错误,您需要用viewcontroller类的实例调用您的方法,即:[myViewController myMethod];如果这就是你的意思。我将在我的回答中添加一些代码。它不起作用,它说:“UIViewController”没有可见的@interface声明选择器“stop”(stop是我试图调用的方法),这是我的ViewController.h-(void)stop;好的,那么在方法applicationIdentinterBackground中有什么代码?我试过[我的方法];它给了我一个错误,您需要用viewcontroller类的实例调用您的方法,即:[myViewController myMethod];如果这就是你的意思。我将在我的回答中添加一些代码。它不起作用,它说:“UIViewController”没有可见的@interface声明选择器“stop”(stop是我试图调用的方法),这是我的ViewController.h-(void)stop;在他的问题中,@AwesomeTN特别问了“我如何使该调用成为我的ViewController类中的方法”…在他的问题中,@AwesomeTN特别问了“我如何使该调用成为我的ViewController类中的方法”…好的,我将其添加到我的ViewController的viewDidLoad中,现在我还能做什么呢?上面代码中的选择器是在收到通知后要调用的函数的名称。当你的应用程序转到后台时,该函数将自动调用。好的,我将其添加到我的viewController的viewDidLoad中,现在我还可以做什么?上面代码中的选择器是收到通知后要调用的函数的名称。当应用程序转到后台时,将自动调用该函数。