如何删除我添加到navigationcontroller视图的视图?

时间:2013-06-03 07:56:04

标签: ios view uinavigationcontroller

所以我的导航控制器中有一个设置栏按钮项,当从家庭视图控制器按下时,会在家庭视图上打开设置vc透明视图,因此家庭vc仍然可以在设置vc后面看到。我希望导航栏仍然显示,所以在“HomeViewController.h”中我有以下代码:

-(IBAction)settingsButtonPressed:(id)sender{
    SettingsViewController *settings = [[SettingsViewController alloc]init];
    [self.navigationController.view addSubview:settings.view];
}

当我想删除设置视图时,在“SettingsViewController”中我尝试了:

-(IBAction)exitSettings:(id)sender{
    [self.navigationController.view removeFromSuperview];
}

但是当我这样做并尝试运行程序时,程序停止,在调试区域,它只是说

Thread 1: EXC_BAD_ACCESS (code = 2, address=0xb0000008)
(lldb)

我做错了什么,如何解决这个问题?

3 个答案:

答案 0 :(得分:6)

由于此代码发生了崩溃:

[self.navigationController.view removeFromSuperview];

您正尝试删除navigationController视图而非设置视图。

当您添加SettingsViewController添加tag时:

-(IBAction)settingsButtonPressed:(id)sender
{
    SettingsViewController *settings = [[SettingsViewController alloc]init];
    settings.view.tag = 7;
    [self.navigationController.view addSubview:settings.view];
}

并使用此tag从navigationcontroller中删除视图:

-(IBAction)exitSettings:(id)sender
{
    [[self.navigationController.view viewWithTag:7] removeFromSuperview];
}

答案 1 :(得分:0)

for (UIView *view in self.navigationController.view.subviews)
{
    if ([view isKindOfClass:[SettingsViewController class]])
    {
        [view removeFromSuperview];
    }
}

答案 2 :(得分:-3)

-(IBAction)exitSettings:(id)sender
{
      [vc dismissModalViewControllerAnimated:YES];
}

vc是您的ViewController对象。

尝试这个。