【问题标题】:UIView continues to exist after removeFromSuperview在 removeFromSuperview 之后 UIView 继续存在
【发布时间】:2010-12-03 14:34:13
【问题描述】:

我在 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];
}

那我要切换到第二个视图:

#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

然后从 secondView 切换到 firstView 也是如此……

问题是当我切换视图时,我想释放的总是可见且不会消失。

Download the full code

【问题讨论】:

    标签: iphone memory-management uiview uiviewcontroller


    【解决方案1】:
    [mainViewController.view removeFromSuperview];
    [self.view addSubview:secondViewController.view];
    

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

    【讨论】:

    • 感谢您的回复。我需要在主屏幕上添加 secondView.xib,这就是我要求 self.view 添加的原因。这是错的吗 ?已创建属性 mainViewController 以删除与使用的 MainViewController 关联的 firstView.xib。因为我无法在没有属性的情况下删除 xib。我没有找到答案,但仍在探索中……
    【解决方案2】:

    更好的方法可能是创建一个您添加到窗口的顶级父视图控制器。它应该引用您的其他两个视图控制器并进行交换。如果您希望每个视图中的按钮引起交换,您可以只为每个帖子的按钮 IBAction 设置一个通知,您的顶级视图注册并响应。

    【讨论】:

    • 感谢您的回复。我也会尝试这种方式,但需要更多帮助。如果我使用 IB 将视图控制器添加到窗口中,是否还需要在 MainWindow.xib 中添加一个视图作为顶级视图?
    • 最后我采用了这种方法。
    • 您找到问题的答案了吗?这提供了一种不同的方法,但似乎并没有直接回答什么不起作用。我正在尝试找出与您类似的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 2012-07-17
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多