关闭应用程序后如何执行操作?

时间:2013-11-16 21:31:02

标签: ios delegates performselector

如何在按下主页按钮后执行操作? 我想我必须在AppDelegate中添加一些代码? 有可能做一些像?:

的代码
[self performSelector:@selector(selector)];

提前致谢!

2 个答案:

答案 0 :(得分:2)

在appDelegate.m中使用applicationWillResignActive:方法

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to 
    // inactive state. This can occur for certain types of temporary 
    // interruptions (such as an incoming phone call or SMS message) 
    // or when the user quits the application and it begins the 
    // transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, 
    // and throttle down OpenGL ES frame rates. Games should use 
    // this method to pause the game.
}

或者第二种选择是使用applicationDidEnterBackGround:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, 
    // invalidate timers, and store enough application state information 
    // to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, 
    // this method is called instead of applicationWillTerminate: when the user quits.
}

取决于当然,你想做什么。这些评论是Apple代码模板的直接引用。


访问视图控制器的方法:

MyViewController *vController = (MyViewController *)[_window rootViewController];
[vController pause];

您的错误告诉您,您的App Delegate类中没有pause方法。

答案 1 :(得分:0)

斯威夫特3:

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data,invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}