主页按钮事件

时间:2013-01-09 05:01:46

标签: iphone objective-c

当用户按下主页按钮时,我不想关闭我的应用程序。相反,我需要返回到重新打开应用程序后离开的同一页面。

5 个答案:

答案 0 :(得分:1)

如果您使用的是其他视图控制器,请使用此功能进行关闭按钮。

旧版本:

-(IBAction)Close:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

对于iOS6:

-(IBAction)closebtn:(id)sender{
{
    [self dismissViewControllerAnimated:YES completion:Nil];
}

它会自动将您重定向到视图控制器,从那里导航到当前视图控制器。

答案 1 :(得分:0)

我认为你试图在应用程序进入后台时保存应用程序的状态,然后在前台时检索它。试着阅读NSUserDefaults。这是文档link

答案 2 :(得分:0)

这些是你的选择

在你的app delegate:

- (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.
}

- (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.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

当用户按下主页按钮时,会按特定顺序调用这些方法。您的申请不会停止。它在后台。您可以在这些函数中编写逻辑,以确保您的应用程序在停止的同一个类中打开。

答案 3 :(得分:0)

iPhone / iPad主页按钮始终关闭当前应用程序并返回主屏幕。应用程序无法覆盖此行为。

答案 4 :(得分:0)

我假设你在谈论实体主页按钮。 Apple沙箱除了自己的所有应用程序,因此您无法访问iDevices的某些方面,包括所有硬件按钮。如果您希望应用程序返回用户在进入后台后停止的位置,您将使用委托文件中的方法,即

- (void)applicationDidEnterBackground:(UIApplication *)application {// your code}

如果你的问题仅仅是回到用户不同意义上的地方(又是物理主页按钮)......

与Zen说的一样,如果您希望在下次用户填写时填写某个文本字段,则可以使用用户默认值。即。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (!defaults) {
    [self setDefaults]; // where setDefaults is just a method that set some random values
}

int apples = [[NSUserDefaults standardUserDefaults] integerForKey:@"apples"];
if (apples == 0) {
    apples = 2;
}

// fill in the interface values for xibApples, a UITextField outlet
[xibApples setText:[NSString stringWithFormat:@"%d", apples]];