【问题标题】:in App Delegate do I need to release my "window" and "navigationController"?在 App Delegate 中,我需要释放我的“window”和“navigationController”吗?
【发布时间】:2011-09-04 13:25:45
【问题描述】:

在 App Delegate 中我:

  1. 需要释放我的“window”和“navigationController”吗?和
  2. 我应该从 (a) applicationDidReceiveMemoryWarning 和 (b) dealloc 中释放它?

代码清单

@interface weekendviewerAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow                        *window;
    UINavigationController          *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@implementation weekendviewerAppDelegate
@synthesize window;
@synthesize navigationController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
    rootViewController.managedObjectContext = self.managedObjectContext;
    self.window.rootViewController = self.navigationController;

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];

    return YES;
}


.
.

【问题讨论】:

  • 如果您从基于导航的应用程序模板开始,您会看到它们确实在 dealloc 中发布。

标签: iphone ios memory-management uinavigationcontroller uiapplicationdelegate


【解决方案1】:

正如 Bolt 时钟所说,您需要在 appDelegate 类中添加一个 dealloc 方法。

- (void)dealloc {

    [navigationController release];
    [window release];
    [super dealloc];
}

【讨论】:

    【解决方案2】:

    如果你曾经在applicationDidReceiveMemoryWarning 中释放你的窗口或导航控制器@greg,你不认为当你的应用程序收到内存警告时你的应用程序会崩溃。

    正如@Bolt 和@ishu 所说,您只需要在 dealloc 方法中释放它。

    同样在applicationDidReceiveMemoryWarning 方法中你可以释放那些在某个时间点后不会被使用的类变量,因为释放它们可能会导致你的应用程序崩溃。

    因此,请明智地选择哪些不重要的变量可能会导致您的应用程序崩溃或阻止您的应用程序正常工作。

    【讨论】:

      猜你喜欢
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2012-01-22
      • 2013-07-31
      相关资源
      最近更新 更多