2

所以我的导航控制器中有一个设置栏按钮项,当从主视图控制器按下时,会在主 vc 上打开一个设置 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)

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

4

3 回答 3

7

由于这段代码,这里发生了崩溃:

[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从导航控制器中删除视图:

-(IBAction)exitSettings:(id)sender
{
    [[self.navigationController.view viewWithTag:7] removeFromSuperview];
}
于 2013-06-03T08:12:49.653 回答
0
for (UIView *view in self.navigationController.view.subviews)
{
    if ([view isKindOfClass:[SettingsViewController class]])
    {
        [view removeFromSuperview];
    }
}
于 2013-06-03T08:12:55.680 回答
-3
-(IBAction)exitSettings:(id)sender
{
      [vc dismissModalViewControllerAnimated:YES];
}

vc 是你的 ViewController 对象。

试试这个。

于 2013-06-03T08:15:52.260 回答