我有一个UIViewController,可以通过两个不同的其他视图来呈现。这是一个搜索表单,对两个视图的作用完全相同。
假设 View1 和 View2 可以执行呈现 SearchView 实例的 segue。我如何知道哪个视图显示了 SearchView,以便我可以执行链接到相应操作的展开 segue?
由于 View1 和 View2 都是 TabViews,我不想在每个视图中使用导航控制器,因为我发现对于一个简单的任务来说它有点过分。
在此示例中:
视图1.m
-(IBAction)didClickButton:
{
[self performSegueWithIdentifier:@"searchSegue1"];
}
视图2.m
-(IBAction)didClickButton:
{
[self performSegueWithIdentifier:@"searchSegue2"];
}
搜索视图
// Here is where I should know which segue to call
if (***ConditionToSegue1***)
[self performSegueWithIdentifier:@"unwindToSegue1"];
else if (***ConditionToSegue2***)
[self performSegueWithIdentifier:@"unwindToSegue2"];
这是什么条件?最好的方法是在 prepareForSegue 方法中设置 SearchView 的属性,告诉我发起调用的视图是什么?请记住,我不希望使用导航控制器。
此致敬意
在 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"];
首先,您应该知道您是在使用故事板还是以编程方式为执行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)
在您的情况下,它可能像[self dismissViewControllerAnimated: YES completion: nil];
一样简单
看看这篇靠近结尾的博客文章,标题为"放松Segues"。
http://chrisrisner.com/Unwinding-with-iOS-and-Storyboards
您可以将动作连接到情节提要上的Exit
组件,这将为您展开。
-(IBAction)reset:(UIStoryboardSegue *)segue {
//do stuff
}
我发现这个问题有一个简单的答案。显然,如果我在两个名称相同的视图控制器上创建一个展开操作,我也可以为它们创建一个展开 segue。
如果我在 SearchView 上执行此 segue,它会根据谁提出它来调用正确的操作。
这似乎暂时有效,我真的不知道为什么或如何发生。如果有人能解释一下,将不胜感激。
更新:不,它没有按预期工作。尽管提供了正确的视图,但会调用这两个操作,从而导致不需要的行为。