进入新界面后如何销毁老界面

iOS 码拜 9年前 (2015-05-11) 1356次浏览 0个评论
 

[self presentViewController:vc animated:YES completion:^{  }]
请问通过这个进入新界面后如何销毁此界面?

NSMutableArray* array = [self.navigationController.viewControllers mutableCopy];
[array removeObjectAtIndex:array.count-2];
NSLog(@”%ld,%ld”,self.navigationController.viewControllers.count,array.count);
self.navigationController.viewControllers = array;
NSLog(@”—-%ld”,self.navigationController.viewControllers.count);
这是通过push的,我在viewDidLoad方法这样写,结果第一次输出“2,1”,第二次输出2 ,不是应该是1吗,那个界面也没销毁

push本来就不销毁啊,用 present 切换才是销毁的
引用 1 楼 Damon_hoo 的回复:

push本来就不销毁啊,用 present 切换才是销毁的

但push完我需要销毁,原来的界面用不上了,present也不销毁,但我也要销毁

引用 2 楼 qq_25532387 的回复:
Quote: 引用 1 楼 Damon_hoo 的回复:

push本来就不销毁啊,用 present 切换才是销毁的

但push完我需要销毁,原来的界面用不上了,present也不销毁,但我也要销毁

假设你有两个VC,一个是VC1,一个是VC2,想进入VC2的时候把VC1释放掉是可以的(例如一些 app 的登录页面),这种情况下,在VC1的时候不要直接push VC2,而是通过navigationController的setViewControllers去手动管理VC栈,如:

NSMutableArray *arr = self.navigationController.viewControllers.mutableCopy
/* arr remove VC1 */
/* arr add VC2 */
[self.navigationController setViewControllers:arr animated:YES];
引用 3 楼 zhangao0086 的回复:

NSMutableArray *arr = self.navigationController.viewControllers.mutableCopy
/* arr remove VC1 */
/* arr add VC2 */
[self.navigationController setViewControllers:arr animated:YES];
[/code]

我这样操作了,navi里没有了,但是push后还是没执行dealloc方法,是其他地方的问题吗

40分
引用 4 楼 qq_25532387 的回复:
Quote: 引用 3 楼 zhangao0086 的回复:

NSMutableArray *arr = self.navigationController.viewControllers.mutableCopy
/* arr remove VC1 */
/* arr add VC2 */
[self.navigationController setViewControllers:arr animated:YES];
[/code]

我这样操作了,navi里没有了,但是push后还是没执行dealloc方法,是其他地方的问题吗

你的 VC1 有内存泄漏,检查下

好的,谢谢,我再检查下
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"登录";
    
    [self createUI];
}

-(void)createUI{
    self.userNameTF = [[UITextField alloc]initWithFrame:CGRectMake(44, 100, ScreenWidth-44*2, 40)];
    self.userNameTF.backgroundColor = [UIColor whiteColor];
    self.userNameTF.placeholder = @"用户名";
    self.userNameTF.borderStyle = UITextBorderStyleBezel;
    self.userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.userNameTF.delegate = self;
    [self.view addSubview:self.userNameTF];
    
    self.passwordTF = [[UITextField alloc]initWithFrame:CGRectMake(_userNameTF.left, _userNameTF.bottom+20, _userNameTF.width, _userNameTF.height)];
    self.passwordTF.backgroundColor = [UIColor whiteColor];
    self.passwordTF.placeholder = @"密码";
    self.passwordTF.borderStyle = UITextBorderStyleBezel;
    self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.delegate = self;
    [self.view addSubview:self.passwordTF];
    
    UIButton* loginButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2 - 50, _passwordTF.bottom+20, 100, 44)];
    loginButton.backgroundColor = [CustomColor blue];
    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [loginButton setTitle:@"登 录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
}

//登录
-(void)login{
    if ([self.userNameTF.text isEqualToString:@""]){
        [super showHUD:@"请输入用户名!"];
        return;
    }
    else if ([self.passwordTF.text isEqualToString:@""]){
        [super showHUD:@"请输入密码!"];
        return;
    }
    
    [super showHUD:@"登录中..." isDim:NO];
    [GUtils loginUserByUserName:self.userNameTF.text andPassword:self.passwordTF.text completion:^(id obj) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [super removeHUD];
            if ([obj isEqualToString:@"1"]) {
                //登录成功
                HomeViewController* vc = [[HomeViewController alloc]init];
                NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
                [array removeObject:self];
                [array addObject:vc];
                [self.navigationController setViewControllers:array animated:YES];
            }
            else if ([obj isEqualToString:@"-2"]) {
                [super showHUD:@"账号不存在!"];
            }
            else if ([obj isEqualToString:@"-1"]) {
                [super showHUD:@"密码错误!"];
            }
            else if ([obj isEqualToString:@"null"]) {
                [super showHUD:@"系统繁忙,请稍候重试!"];
            }
        });
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.userNameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
}

#pragma mark -TextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

这是我登录界面的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [CustomColor lightGray];
    
    if (self.navigationController.viewControllers.count > 1 ) {
        UIButton *button = [[UIButton alloc]init];
//        [button setImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"return_on.png"] forState:UIControlStateHighlighted];
        [button setTitle:@"后退" forState:UIControlStateNormal];
        button.backgroundColor = [CustomColor deepGray];
        button.frame = CGRectMake(0, 0, 44, 44);
        [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = backItem;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

//设置导航栏上的标题
- (void)setTitle:(NSString *)title {
    [super setTitle:title];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = title;
    [titleLabel sizeToFit];
    
    self.navigationItem.titleView = titleLabel;
}

#pragma mark -HUD
-(void)showHUD:(NSString*)title{
    //如果之前有,先移除
    if (self.hud) {
        [self.hud removeFromSuperview];
    }
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    self.hud.customView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    self.hud.customView = [[UIView alloc]init];
    self.hud.mode=MBProgressHUDModeCustomView;
    self.hud.labelText=title;
    [self.hud hide:YES afterDelay:1];
}

-(void)showHUD:(NSString*)title withDelay:(CGFloat)time{
    //如果之前有,先移除
    if (self.hud) {
        [self.hud removeFromSuperview];
    }
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //    self.hud.customView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    self.hud.customView = [[UIView alloc]init];
    self.hud.mode = MBProgressHUDModeCustomView;
    self.hud.labelText = title;
    [self.hud hide:YES afterDelay:time];
}

-(void)showHUD:(NSString *)title isDim:(BOOL)isDim {
    //如果之前有,先移除
    if (self.hud) {
        [self.hud removeFromSuperview];
    }
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.dimBackground = isDim;
    self.hud.labelText = title;
}

-(void)removeHUD{
    if (self.hud) {
        [self.hud removeFromSuperview];
    }
}

-(void)dealloc{
    NSLog(@"%@",[NSString stringWithFormat:@"%@销毁",[self class]]);
}

这是登录界面继承的类的代码   ,不知道哪里内存泄露了,界面不销毁,push出去然后pop出来的部分界面也不销毁,是这个类的问题吗

这样看似乎没有, 你通过注释代码来做点测试吧,比如把 login 里的方法都注释掉,只调用登录成功后的那段:
//登录成功
HomeViewController* vc = [[HomeViewController alloc]init];
NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
[array removeObject:self];
[array addObject:vc];
[self.navigationController setViewControllers:array animated:YES];

看有没有被释放,如果这里没有的话,在其他方法里再试试

引用 9 楼 zhangao0086 的回复:

这样看似乎没有, 你通过注释代码来做点测试吧,比如把 login 里的方法都注释掉,只调用登录成功后的那段:
//登录成功
HomeViewController* vc = [[HomeViewController alloc]init];
NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
[array removeObject:self];
[array addObject:vc];
[self.navigationController setViewControllers:array animated:YES];

看有没有被释放,如果这里没有的话,在其他方法里再试试

注释掉可以了,请求出了问题,我再研究下,谢谢

引用 10 楼 qq_25532387 的回复:
Quote: 引用 9 楼 zhangao0086 的回复:

这样看似乎没有, 你通过注释代码来做点测试吧,比如把 login 里的方法都注释掉,只调用登录成功后的那段:
//登录成功
HomeViewController* vc = [[HomeViewController alloc]init];
NSMutableArray *array = [self.navigationController.viewControllers mutableCopy];
[array removeObject:self];
[array addObject:vc];
[self.navigationController setViewControllers:array animated:YES];

看有没有被释放,如果这里没有的话,在其他方法里再试试

注释掉可以了,请求出了问题,我再研究下,谢谢

祝早日解决


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明进入新界面后如何销毁老界面
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!