1

所以,我正在阅读这个相关的 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 类对象,所以一个是/正在按预期工作。

4

2 回答 2

1

You should add a condition around the line that sets the incoming segue:

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

The idea is to call setIncomingSegue: only on the destination view controller that supports your added method.

You should also change the code in the destination view controller to check string equality with isEqualToString:

if ([_incomingSegue isEqualToString:@"add"])
{
    //snipped code here
}
else if ([_incomingSegue isEqualToString:@"edit"])
{
    //snipped code here
}
于 2012-12-12T01:31:57.753 回答
0

When you call @synthesize incomingSegue = _incomingSegue;

You really only need to call

@synthesize incomingSegue;

于 2016-03-10T03:06:35.250 回答