applicationWillTerminate具有多个视图

时间:2010-03-07 17:42:30

标签: ios iphone xcode

我正在尝试从applicationWillTerminate上加载的视图中保存我的数据,但遇到了一些问题。

我有四种观点: inFinateViewController FirestViewController SecondViewController ThirdViewController

我正在通过infinateViewController加载我的观点(感谢Mauricio Giraldo代码):

- (void) displayView:(int)intNewView {

NSLog(@"%i", intNewView);   

[self.currentView.view removeFromSuperview];
[self.currentView release];
switch (intNewView) {
    case 1:
        self.currentView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
        break;
    case 2:
        self.currentView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
        break;
    case 3:
        self.currentView = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
        break;
}

[self.view addSubview:self.currentView.view];

CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionReveal]; 
[animation setDuration:0.75];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
[[self.currentView.view layer] addAnimation:animation forKey:kCATransition];

}

它通过以下方式启动:

- (void)viewDidLoad {
self.currentView = [[FirstViewController alloc]
               initWithNibName:@"FirstView" bundle:nil];
[self.view addSubview:self.currentView.view];
[super viewDidLoad];
}

在我的SecondViewController中,我有一个NSMutableArray,在开始时,我用数字初始化,加扰它们,然后使用。我的目标是在调用applicationWillTerminate时保存这些条目。

在我的SecondViewController中,我还有一个方法:

-(void) saveExitData
{
[pnames writeToFile:[self saveFilePath: @"gameArrayData.plist"] atomically:YES];

 }

我的问题是applicationWillTerminate在appDelegate中,虽然我正在调用它,但它似乎没有运行。我试图通过调试器进入它,但它只能击中它,而不会跳进它。代码是:

- (void)applicationWillTerminate:(UIApplication *)application {
[secondViewController saveExitData];

} 

我的infinateViewsAppDelegate如下所示:

//  InfiniteViewsAppDelegate.h
#import <UIKit/UIKit.h>
@class SecondViewController;
@class InfiniteViewsViewController;

@interface InfiniteViewsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
InfiniteViewsViewController *viewController;
SecondViewController  *secondViewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet InfiniteViewsViewController *viewController;
@property (nonatomic, retain) IBOutlet SecondViewController *secondViewController;

-(void) displayView:(int)intNewView;

@end

和.m文件:

//  InfiniteViewsAppDelegate.m
#import "SecondViewController.h"
#import "InfiniteViewsAppDelegate.h"
#import "InfiniteViewsViewController.h"

@implementation InfiniteViewsAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize secondViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[window addSubview:viewController.view];

[window makeKeyAndVisible];

return YES;
 }

- (void)displayView:(int)intNewView {
[viewController displayView:intNewView];
}

- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end

所以,我想我的问题是,我怎样才能保存这些数据? applicationWillTerminate可以放在我的SecondViewController中,这样我就可以编写数据并从那里保存了吗?或者applicationWillTerminate仅存在于appDelegate?

感谢任何能够对此有所了解的人。我已经阅读了书籍上的书籍并且用Google搜索了...但仍然没有答案。

...地理位置

2 个答案:

答案 0 :(得分:3)

如果你的app代理设置正确,那么肯定应该调用applicationWillTerminate:。你应该对此进行调查,因为如果不是这样,你就会在某个地方出错。

没有必要解决您的问题:除了applicationWillTerminate:之外,UIApplication实例还会在退出时发出名为UIApplicationWillTerminateNotification的通知。您可以让视图控制器观察此通知,然后您的应用程序委托不需要维护对视图控制器的引用。

创建视图控制器后,让它注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(applicationWillTerminateNotification:)
                                             name:UIApplicationWillTerminateNotification
                                           object:[UIApplication sharedApplication]];

然后,通知中心将调用您指定的选择器,您可以使用该方法保存您的内容:

- (void)applicationWillTerminateNotification:(NSNotification *)notification {
    // save stuff ...
}

取消分配视图控制器时,不要忘记取消注册:

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIApplicationWillTerminateNotification 
                                              object:[UIApplication sharedApplication]];

答案 1 :(得分:0)

我的代码中没有applicationWillTerminate - 它应该在InfiniteViewsAppDelegate.m中。这就是它将被看到和调用的地方。

Ole Bergemann对通知是正确的,这是一个很好的解决方案。如果您不想使用它们,那么让您的应用程序委托告诉每个视图清理自己也很容易(因为它恰好已经拥有了每个视图的属性)。只需确保您的InfiniteViewsAppDelegate实际设置为应用程序的委托,并确保applicationWillTerminate在InfiniteViewsAppDelegate.m中是正确的。