UIView在removeFromSuperview之后仍然存在 [英] UIView continues to exist after removeFromSuperview

查看:161
本文介绍了UIView在removeFromSuperview之后仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AppDelegate中添加了一个firstView:

I add a firstView in the AppDelegate :

#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];
}

然后我要切换到secondView:

Then I want to switch to the secondView :

#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 ...

And then the same thing for switching from secondView to firstView…

问题是当我切换要释放的视图时始终可见并且不会消失.

The problem is when I switch the view I thought to release is always visible and don't disappear.

下载完整代码

推荐答案

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

A better approach might be to create one top-level parent view controller that you add to the window. It should have a reference to both of your other view controllers and do the swapping instead. If you want your button inside each view to cause the swapping, you could just have your button IBAction for each post a notification that your top level view registers for and responds to.

这篇关于UIView在removeFromSuperview之后仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆