ios - applicationWillTerminate 有多个 View

标签 ios iphone xcode

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

我有四种看法: 在 FinateViewController 中 FirestView Controller 第二 View Controller 第三 View Controller

我正在通过 infinateViewController 加载我的 View (感谢 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 中?

感谢任何可以阐明这一点的人。我读了又读又用谷歌搜索……但仍然没有答案。

地理...

最佳答案

如果您的应用委托(delegate)设置正确,applicationWillTerminate: 肯定会被调用。您应该对此进行调查,因为如果不是,则说明某处有错误。

虽然没有必要解决您的问题:除了 applicationWillTerminate: 之外,UIApplication 实例还会在退出时发送名为 UIApplicationWillTerminateNotification 的通知。您可以让您的 View Controller 观察此通知,然后您的应用委托(delegate)就无需维护对您的 View Controller 的引用。

创建 View Controller 时,让它注册通知:

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

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

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

不要忘记在 View Controller 被释放时注销自己:

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

关于ios - applicationWillTerminate 有多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2397191/

相关文章:

iphone - 使用 Xcode 4.6.1 在 iOS 6 上进行 RTSP 流式传输

cocoa-touch - 运行时 xib 下载?

objective-c - 捆绑 ID 和 SKU 编号

ios - 使用 SecItemCopyMatching swift 2.0 导出由 SecKeyGeneratePair 生成的公钥

iphone - 如何检测 UIImageView 的宽度和高度

ios - NSRegularExpression 内存占用

iphone - 使用performSelectorInBackground运行加载指示器

ios - Xcode 代码完成自定义描述

iphone - iPhone/iPad 应用程序启动时发出警报

ios - 我想记住一个 (NSIndexPath *)indexPath 以便以后删除,但是崩溃了