作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用最新的 SDK 和 XCode 4.2 开发 iOS 4(我不使用ARC)。
我正在以编程方式开发导航 Controller ,我有一个问题。
这是AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@class SecondViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController* navController;
ViewController* viewController;
SecondViewController* secondViewController;
}
@property (strong, nonatomic) UIWindow *window;
- (void) showSecondViewController;
@end
这是 AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.title = @"Menu";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (void) dealloc
{
[_window release];
[viewController release];
[navController release];
[secondViewController release];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
...
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
...
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
...
}
- (void)applicationWillTerminate:(UIApplication *)application
{
...
}
- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.title = @"Second";
[navController pushViewController:secondViewController animated:YES];
}
我的问题是关于最后一个方法,-(void)showSecondViewController;
我可以在最后添加这一行吗?
[secondViewController release]
我已经分析了该应用程序,并且没有发现任何内存泄漏。但我必须在这里问,因为我不确定。
最佳答案
如果再次调用showSecondViewController
方法,将会出现内存泄漏。
您应该在 showSecondViewController
方法中释放 secondViewController
。
- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.title = @"Second";
[navController pushViewController:secondViewController animated:YES];
[secondViewController release]
}
当您执行 pushViewController:secondViewController
navController
保留
关于iphone - 推送 View Controller : Is this a memory leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677490/
我是一名优秀的程序员,十分优秀!