XCode Storyboard:如何在 segue 触发器不在故事板上时创建 segue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10492285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:13:25  来源:igfitidea点击:

XCode Storyboard: how to create a segue when the segue trigger is not on storyboard

xcodestoryboarduistoryboardsegue

提问by vikmalhotra

So I started playing with storyboards in XCode 4.3.2. I started with the Master-Detail application (split view application for iPad). In the detail view (named DetailVC) I have a view where I displayed an array of custom views.

所以我开始在 XCode 4.3.2 中使用故事板。我从 Master-Detail 应用程序(适用于 iPad 的拆分视图应用程序)开始。在详细视图(名为DetailVC)中,我有一个视图,其中显示了一组自定义视图。

The custom views (named GridView) have a tap gesture recognizer which is supposed to handle the tap event on each custom view. Tapping a GridViewpushes a view controller show some search results (named SearchResultsVC).

自定义视图(名为GridView)有一个点击手势识别器,它应该处理每个自定义视图上的点击事件。点击 aGridView推动视图控制器显示一些搜索结果(名为SearchResultsVC)。

With GridViewcreated in a separate nib file, and the DetailVCand SearchResultsVCreside in storyboard, how can I create a push segue with destination SearchResultsVC? I just created a segue between DetailVC and SearchResultsVC? Is there someway I can trigger this segue programatically from inside the GridView class when tap gesture is recognized????

随着GridView在一个单独的榫文件创建的,DetailVC并且SearchResultsVC存在于故事板,我怎么可以创建目的地推SEGUE SearchResultsVC?我刚刚在 DetailVC 和 SearchResultsVC 之间创建了一个 segue?有什么办法可以在识别点击手势时从 GridView 类内部以编程方式触发这个 segue????

回答by Gaz Long

In the method where you handle the tap use:

在您处理水龙头的方法中:

[self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];

[self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];

In your StoryBoard control drag from your DetailVC to your SearchResultVC and choose what type of segue you would like. Make sure to name your segue identifier the same as the one in the method above in attributes inspector.

在您的 StoryBoard 控件中,从 DetailVC 拖动到 SearchResultVC 并选择您想要的转场类型。确保将您的 segue 标识符命名为与属性检查器中上述方法中的相同。

I'm gonna try and improve my answer I messed it up I think:

我会尝试改进我的答案我把它搞砸了我想:

1) In your DetailVC.h create an instance variable for your GridView like this

1)在您的 DetailVC.h 中为您的 GridView 创建一个实例变量,如下所示

IBOutlet UIView * gridView;

also create a getter method and an IBAction for your grid view like this

还为您的网格视图创建一个 getter 方法和一个 IBAction,如下所示

-(UIView *)gridView;
-(IBAction)myGridGotPressed:(id)sender;

2)Now in your DetailVC.m implement your methods like this

2)现在在你的 DetailVC.m 中实现你这样的方法

-(UIView *)gridView{
if(!gridView){
[[NSBundle mainBundle] loadNibNamed:@"GridView" owner:self options:nil];
}
return gridView;
}

Also implement your IBAction like this

也像这样实现你的 IBAction

-(IBAction)myGridGotPressed:(id)sender{
[self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];
}

3) To make this work you need to change the filesOwner class of your GridView to DetailVC and then hook up the outlets and Actions as normal.

3) 要完成这项工作,您需要将 GridView 的 filesOwner 类更改为 DetailVC,然后照常连接插座和操作。

I hope that helps.

我希望这有帮助。