iphone - UIView在removeFromSuperview后继续存在

标签 iphone memory-management uiview uiviewcontroller

我在 AppDelegate 中添加一个firstView:

#import "TestViewAppDelegate.h"
#import "MainViewController.h"
@implementation TestViewAppDelegate

@synthesize window;
@synthesize mainViewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    self.mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window makeKeyAndVisible];
    [window addSubview:mainViewController.view];
}

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

然后我想切换到第二个View:

#import "MainViewController.h"
#import "SecondViewController.h"

@implementation MainViewController

@synthesize mainViewController, secondViewController;

- (IBAction)viewSwitch
{
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    self.secondViewController = second;
    [second release];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

    [mainViewController.view removeFromSuperview];
    [self.view addSubview:secondViewController.view];

    [UIView commitAnimations];
}

- (void)dealloc {
    [mainViewController release];
    [secondViewController release];
    [super dealloc];
}

@end

然后从第二个 View 切换到第一个 View 也是同样的事情......

问题是当我切换 View 时我认为要释放的 View 始终可见并且不会消失。

Download the full code

最佳答案

[mainViewController.view removeFromSuperview];
[self.view addSubview:secondViewController.view];

这里的“ self ”是什么?为什么 MainViewController 类有一个名为 mainViewController 的属性?探索这些问题可能会让您在这里找到答案。

关于iphone - UIView在removeFromSuperview后继续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1514205/

相关文章:

java - 单线程应用程序的 GC 设置

ios - 如何在自定义 UIView 中连接 UICollectionView - Swift

iphone - 在 iOS 上学习 OpenGLES 2.0

iphone - UIView内容错位

iphone - 如何将颜色从一种颜色空间转换为另一种颜色空间?

ios - UITextfield 子类没有响应 insertText : method of UIKeyInput protocol

ios - 如果我有 IBOutlet 约束,复杂的 UIView 布局会有所不同

iphone - 排除 Google 日历中周期性事件的日期

c++ - 为什么变量的打印地址会在每次执行时打印随机值,即使它是 C 中的逻辑地址?

python - 如何以高效内存的方式使用 SOAP 水?