【问题标题】:Identifying an incoming segue识别传入的 segue
【发布时间】:2016-03-10 12:02:15
【问题描述】:

所以,我正在阅读 this related SO,因为它最终也是我想做的事情。

我在我的destinationViewController 的头文件中添加了一个属性:

@property (nonatomic, strong) NSString *incomingSegue;

我已经在destinationViewController的实现文件中合成了它:

@synthesize incomingSegue = _incomingSegue;

我已将以下几行添加到我的 prepareForSegue 方法中,用于 sourceViewController(取决于触发 segue 的):

[segue.destinationViewController setIncomingSegue:@"edit"];
[segue.destinationViewController setIncomingSegue:@"add"];

最后,我有一个过程来检查我的destinationViewController 的实现文件中设置了哪个值:

if (_incomingSegue == @"add")
    {
        //snipped code here
    }
    else if (_incomingSegue == @"edit")
    {
        //snipped code here
    }

所以,显然我错过了一些东西。当我尝试执行 segue 时,我得到一个错误,在 SO 中出现了大约 1000 次,这使得很难弄清楚我忽略了哪些细节。这件事在我的 sourceViewController 上的 prepareForSegue 方法中触发(根据断点):

无法识别的选择器发送到实例

我可以不使用文字字符串 (@"string") 代替 (NSString *),或者是其他什么东西引发错误?

更新(已解决):

我的 prepareForSegue 方法更详细的描述:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"editSegue"])
    {
        //snipped
        [segue.destinationViewController setIncomingSegue:@"edit"];
    }

    else if ([[segue identifier] isEqualToString:@"addSegue"])
    {
        //snipped
        DestinationViewController *dtv = (DestinationViewController *)[[segue destinationViewController]topViewController];
        [dvc setIncomingSegue:@"add"]
    }
}

事实证明,我必须使用我声明的 DestinationViewController 类对象来设置值。而不是像我对 editSegue 所做的那样仅引用 segue.destinationViewController。我没有为 editSegue 声明 DestinationViewController 类对象,因此它按预期工作。

【问题讨论】:

    标签: objective-c ios


    【解决方案1】:

    您应该在设置传入 segue 的行周围添加一个条件:

    if ([segue.identifier isEqualToString:@"SegueToControllerThatSupportsIncomingSegue"]) {
        [segue.destinationViewController setIncomingSegue:@"edit"];
    }
    

    这个想法是只在支持您添加的方法的目标视图控制器上调用setIncomingSegue:

    您还应该更改目标视图控制器中的代码以检查与isEqualToString: 的字符串是否相等

    if ([_incomingSegue isEqualToString:@"add"])
    {
        //snipped code here
    }
    else if ([_incomingSegue isEqualToString:@"edit"])
    {
        //snipped code here
    }
    

    【讨论】:

    • 感谢您的回复!我有相同的destinationViewController 以模态方式(使用addSegue)或推送(使用editSegue)显示。希望这有助于解释为什么在两者中都使用了incomingSegue。我还更新了问题以显示更多我当前的 prepareForSegue 方法。
    • 您需要使用isEqualToString:@"add",除了我做错了什么导致错误(请参阅问题中的更新)。我要检查答案,因为它也有助于实现我的目标,谢谢!
    • @WaynePatnode 我明白了 - 本质上,您将 setIncomingSegue 发送到了错误的控制器。请注意,您不需要转换为实际类 - 您可以将最后两行重写为一行,如下所示:[[segue.destinationViewController topViewController] setIncomingSegue:@"add"];
    【解决方案2】:

    当你打电话时 @synthesize incomingSegue = _incomingSegue;

    你真的只需要打电话

    @synthesize incomingSegue;

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 2017-05-15
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多