【问题标题】:iOS 7 - Identify segue that called view to unwind correctlyiOS 7 - 识别调用视图以正确展开的 segue
【发布时间】:2014-03-23 20:28:51
【问题描述】:

我有一个 UIViewController 可以由两个不同的其他视图呈现。这是一个搜索表单,对两种视图的行为完全相同。

假设 View1 和 View2 可以执行显示 SearchView 实例的 segue。我怎么知道哪个视图呈现了 SearchView 以便我可以执行与相应操作相关联的展开转场?

由于 View1 和 View2 都是 TabView,我不想在它们每个中都使用 navigationController,因为我发现它对于一个简单的任务来说有点过于强大了。

在这个例子中:

View1.m

-(IBAction)didClickButton:
{
    [self performSegueWithIdentifier:@"searchSegue1"];
}

View2.m

-(IBAction)didClickButton:
{
    [self performSegueWithIdentifier:@"searchSegue2"];
}

SearchView.m

// Here is where I should know which segue to call
if (***ConditionToSegue1***)
    [self performSegueWithIdentifier:@"unwindToSegue1"];

else if (***ConditionToSegue2***)
    [self performSegueWithIdentifier:@"unwindToSegue2"];

这是什么条件?最好的方法是在 prepareForSegue 方法中设置 SearchView 的属性,告诉我发起调用的视图是什么?请记住,我不想使用 NavigationController。

最好的问候

【问题讨论】:

    标签: ios iphone objective-c uistoryboard uistoryboardsegue


    【解决方案1】:

    在 SearchView.h 中添加行 @property (nonatomic) int tag

    然后在View1.m中添加如下方法:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.identifier isEqualToString:@"searchSegue1"])
            [segue.destinationViewController setValue:1 forKey:@"tag"];
    }
    

    在 View2.m 中做同样的事情,但将 1 替换为 2。然后将 if 语句更改为:

    if (self.tag == 1)
        [self performSegueWithIdentifier:@"unwindToSegue1"];
    else if (self.tag == 2)
        [self performSegueWithIdentifier:@"unwindToSegue2"];
    

    【讨论】:

    • 这是一个好方法。我想过使用这样的东西,但发现了一个更好的似乎可以工作的东西。
    • 不,我的“更好的东西”没有完全奏效。将您的答案标记为正确。
    【解决方案2】:

    首先,您应该知道您是使用情节提要还是以编程方式为 PERFORMING SEGUE 创建情节提要。

        1.If you directly connect the segue through the click button(one view controller to another view controller),you don't need to write your above coding.Just it is enough to give segue name. 
                click->SHOW THE ATTRIBUTES OF RIGHT SIDE UTILITY AREA.
                     ->NOW YOU CAN SEE THE STORYBOARD SEGUE WITH-IDENTIFIER,STYLE 
                     ->IN IDENTIFIER TEXTFIELD JUST GIVE YOUR SEGUE NAME(WHATEVER YOU WANT)
                       (IN STYLE-PUSH IS DEFAULT IF YOU CONNECT THE SEGUE AS A PUSH)
    

    【讨论】:

      【解决方案3】:

      在您的情况下,它可能就像[self dismissViewControllerAnimated: YES completion: nil]; 一样简单

      查看标题为Unwind Segues的这篇博文。

      http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards

      您可以将一个动作连接到情节提要上的 Exit 组件,该组件将为您展开。

      -(IBAction)reset:(UIStoryboardSegue *)segue {
          //do stuff
      }
      

      【讨论】:

        【解决方案4】:

        我发现有一个简单的答案。显然,如果我在两个名称相同的视图控制器上创建一个展开动作,我也可以为它们都创建一个展开序列。

        如果我在 SearchView 上执行此 segue,它会根据谁提供它调用正确的操作。

        这似乎暂时有效,我真的不知道为什么或如何发生。如果有人能解释一下,将不胜感激。

        更新: 不,它没有按预期工作。尽管显示了正确的视图,但这两个动作都被调用,从而导致了不需要的行为。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多