【问题标题】:Removing a viewcontroller by selecting a button [closed]通过选择按钮删除视图控制器 [关闭]
【发布时间】:2012-06-18 04:27:43
【问题描述】:

我创建了一个包含 4 个视图控制器及其 .h、.m 文件的应用程序...在我的第一个视图控制器中,按下一个按钮会转到第二个视图控制器,而在第二个视图控制器中有两个按钮,它用于切换回第一个视图控制器另一个按钮将转到 thirdviewcontroller。这是我的 firstviewcontroller.m 代码

[[NSBundle mainBundle] loadNibNamed:@"SecondViewController" owner:self options:nil];

在我的第二个视图控制器中用于第一个按钮

[[NSBundle mainBundle] loadNibNamed:@"FirstViewController" owner:self options:nil];

还有一个按钮

[[NSBundle mainBundle] loadNibNamed:@"ThirdViewController" owner:self options:nil];

当我在 firstviewcontroller 中选择按钮时,它会加载 secondviewcontroller,但在第二个视图控制器中,如果我选择任何按钮,我会收到 Sigabart 警告...

任何人都可以对此有所了解...我已经尝试了很多方法..

【问题讨论】:

  • 为此使用导航控制器
  • 是否有特定要求以这种方式导航,否则您可以按照 Safecase 的建议使用 UINavigationController。
  • 根据您的要求使用导航控制器

标签: iphone xcode ios5 uiview uiviewcontroller


【解决方案1】:

您可以使用以下方法来执行这些任务:

方法一:

在 FirstViewController.m 中,在按钮单击时编写此代码:

SecondViewController *secondVC = [[SecondViewController alloc]initwithNibName:@"SecondViewController" bundle:nil];
[self.view addSubView:secondVC.view];

这会将第二个视图控制器添加到当前视图

在SecondViewController.m中添加第三个View可以写

ThirdViewController *thirdVC = [[ThirdViewController alloc]initwithNibName:@"ThirdViewController" bundle:nil];
[self.view addSubView:thirdVC.view];

要删除第二个视图,您可以这样写:

[self.view removeFromSuperview];

方法二:

使用导航控制器。

【讨论】:

  • 非常感谢...它的工作原理...
  • 如果代码对您有用,请接受答案并喜欢它。
【解决方案2】:

使用这样的代码

FirstViewController *FVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

//For Push    
[self.navigationController pushViewController:FVC animated:YES];

//For Pop
[self.navigationController popViewControllerAnimated:YES];

:)

【讨论】:

    【解决方案3】:

    在 iPhone 应用程序编程中,当我们将视图控制器推送到新的另一个视图控制器时,视图控制器通常一个一个地存储在堆栈中。并查看以相同的反向格式删除这些控制器。所以如果你想推一个新的视图控制器使用这个:

    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    [self.navigationController pushViewController:second animated:YES];

    如果你想删除当前的视图控制器,使用这个:

    `[self.navigationController popViewControllerAnimated:YES] ;

    如果你想从任何视图控制器跳转到你的第一个视图控制器

    [self.navigationController popToRootViewControllerAnimated:YES];

    你可以通过这行代码得到这个栈中呈现的viewcontrollers

    NSArray *viewArr = [self.navigationController viewControllers];

    NSLog(@"%@",viewArr);

    [self.navigationController popToViewController:[viewArr objectAtIndex:1] animated:YES];

    `

    【讨论】:

      【解决方案4】:
      //For going in forword direction you can use this block
      {
      FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
      [self.navigationController pushViewController:firstVC animated:YES];
      [firstVC relese];
      }
      
      //For returning back you can use this block
      { 
         [self.navigationController popViewControllerAnimated:YES];
      }
      
      
      //But before using this line of code you have to alloc and make the property of    navigationController in your appDelegate
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多