【问题标题】:conditions for navigation between viewcontrollers视图控制器之间导航的条件
【发布时间】:2013-12-27 13:21:06
【问题描述】:

我正在与 ios 项目合作并在其中做得很好。但现在我停留在一个我拥有三个视图的地方(比如 1stview、2ndview、3rdview)。我正在使用下面的代码行从第一个视图和第三个视图导航到第二个视图。

[self.navigationController pushViewController:first view animated:YES];

无论我是从第一视图还是第三视图导航,我如何检查第二视图。这样我就可以使用特定的条件。 所以请帮我解决这个问题。您的帮助将不胜感激。

【问题讨论】:

    标签: ios iphone objective-c uinavigationcontroller


    【解决方案1】:

    在第二个视图控制器中获取一个变量。当您创建它的对象时,请为其设置适当的值。稍后当它被推送时,您可以使用该值做出正确的决定。

    在 FirstViewController 的文件中,您将写下以下几行:

    SecondController *controller = [[SecondController alloc]init];
    controller.flag = 1; //That means you came here from viecontroller 1
    [self.navigationController pushViewController:controller animated:YES];
    

    在 ThirdViewcontroller 的文件中,您将编写以下行:

    SecondController *controller = [[SecondController alloc]init];
    controller.flag = 3; //That means you came here from viecontroller 3
    [self.navigationController pushViewController:controller animated:YES];
    

    【讨论】:

      【解决方案2】:

      尝试安排事情,使 2ndView 不知道 1stView 或 3rdView,而是根据其配置方式更改其行为。因此,假设当您从 1stView 导航到 2ndView 时,2nd 应该以绿色背景显示,而当您从 3rd 到达那里时,它应该使用蓝色代替。而不是告诉 2nd 哪个控制器在它之前,让前面的控制器只告诉 2nd 使用什么背景颜色。 2ndView 行为的任何其他方面也是如此。

      这样做的好处是您可以更改 1st 或 3rd 而无需更改 2nd 中的任何内容,并且您可以稍后添加同样使用 2ndView 的 4thView 或 5thView 而无需更改 2ndView。

      【讨论】:

        【解决方案3】:

        实现方法 - navigationController:willShowViewController:animated: 来自 UINavigationControllerDelegate 协议参考。在此方法中,您可以使用 UINavigationController 的几个属性检查导航堆栈以获取当前视图控制器。一个例子是访问 visibleViewController 属性。

        正如@Apurv 指出的那样,您需要某种标识符机制才能知道调用来自哪个视图控制器。例如:viewController.view.tag

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-10
          • 2014-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多