ios 以编程方式创建 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9674685/
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
Creating a segue programmatically
提问by Tiago Veloso
I have a common UIViewController
that all my UIViewsControllers
extend to reuse some common operations.
我有一个共同点UIViewController
,我所有的UIViewsControllers
扩展都可以重用一些常见的操作。
I want to set up a segue on this "Common" UIViewController
so that all the other UIViewControllers
inherit.
我想在这个“Common”上设置一个segue,UIViewController
以便所有其他人UIViewControllers
继承。
I am trying to figure out how do I do that programmatically.
我想弄清楚如何以编程方式做到这一点。
I guess that the question could also be how do I set a segue
for all my UIViewControllers
without going into the story board and do them by hand.
我想问题也可能是我如何在不进入故事板并手动完成的情况下segue
为我的所有内容设置一个UIViewControllers
。
采纳答案by jonkroll
By definition a segue can't really exist independently of a storyboard. It's even there in the name of the class: UIStoryboardSegue
. You don't create segues programmatically - it is the storyboard runtime that creates them for you. You can normally call performSegueWithIdentifier:
in your view controller's code, but this relies on having a segue already set up in the storyboard to reference.
根据定义,segue 不能真正独立于故事板而存在。它甚至以班级的名义存在:UIStoryboardSegue
. 您不会以编程方式创建 segue - 故事板运行时会为您创建它们。您通常可以调用performSegueWithIdentifier:
视图控制器的代码,但这依赖于已经在故事板中设置的 segue 以供参考。
What I think you are asking though is how you can create a method in your common view controller (base class) that will transition to a new view controller, and will be inherited by all derived classes. You could do this by creating a method like this one to your base class view controller:
我认为您要问的是如何在公共视图控制器(基类)中创建一个方法,该方法将转换为新的视图控制器,并将由所有派生类继承。你可以通过为你的基类视图控制器创建一个像这样的方法来做到这一点:
- (IBAction)pushMyNewViewController
{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
[self presentModalViewController:myNewVC animated:YES];
}
and then in your derived class, call that method when the appropriate button is clicked or table row is selected or whatever.
然后在您的派生类中,当单击适当的按钮或选择表格行或其他任何内容时调用该方法。
回答by smileBot
I thought I would add another possibility. One of the things you can do is you can connect two scenes in a storyboard using a segue that is not attached to an action, and then programmatically trigger the segue inside your view controller. The way you do this, is that you have to drag from the file's owner icon at the bottom of the storyboard scene that is the segueing scene, and right drag to the destination scene. I'll throw in an image to help explain.
我想我会添加另一种可能性。一,你可以做的事情是,你可以使用没有连接到一个动作,然后以编程方式触发您的视图控制器内的一个赛格瑞在赛格瑞故事板连接两个场景。你做的方式,就是你必须在故事板场景是segueing场景的底部,从文件的所有者图标拖动和右键拖动到目标场景。我会放一张图片来帮助解释。
A popup will show for "Manual Segue". I picked Push as the type. Tap on the little square and make sure you're in the attributes inspector. Give it an identifier which you will use to refer to it in code.
将显示“Manual Segue”弹出窗口。我选择 Push 作为类型。点击小方块并确保您在属性检查器中。给它一个标识符,您将使用它在代码中引用它。
Ok, next I'm going to segue using a programmatic bar button item. In viewDidLoad or somewhere else I'll create a button item on the navigation bar with this code:
好的,接下来我将使用程序化的栏按钮项进行切换。在 viewDidLoad 或其他地方,我将使用以下代码在导航栏上创建一个按钮项:
UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
style:UIBarButtonItemStyleDone
target:self
action:@selector(buttonizeButtonTap:)];
self.navigationItem.rightBarButtonItems = @[buttonizeButton];
Ok, notice that the selector is buttonizeButtonTap:. So write a void method for that button and within that method you will call the segue like this:
好的,注意选择器是 buttonizeButtonTap:。因此,为该按钮编写一个 void 方法,在该方法中,您将像这样调用 segue:
-(void)buttonizeButtonTap:(id)sender{
[self performSegueWithIdentifier:@"Associate" sender:sender];
}
The sender parameter is required to identify the button when prepareForSegue is called. prepareForSegue is the framework method where you will instantiate your scene and pass it whatever values it will need to do its work. Here's what my method looks like:
调用 prepareForSegue 时,需要使用 sender 参数来标识按钮。prepareForSegue 是框架方法,您将在其中实例化您的场景并传递它完成其工作所需的任何值。这是我的方法的样子:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Associate"])
{
TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
translationQuizAssociateVC.contentID = self.contentID;
translationQuizAssociateVC.index = self.index;
translationQuizAssociateVC.content = self.content;
}
}
Ok, just tested it and it works. Hope it helps you.
好的,刚刚测试了它,它可以工作。希望对你有帮助。
回答by Jean-Philippe Pellet
I've been using this code to instantiate my custom segue subclass and run it programmatically. It seems to work. Anything wrong with this? I'm puzzled, reading all the other answers saying it cannot be done.
我一直在使用这段代码来实例化我的自定义 segue 子类并以编程方式运行它。它似乎工作。这有什么问题吗?我很困惑,阅读所有其他答案说它无法完成。
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
回答by qrikko
Guess this is answered and accepted, but I just would like to add a few more details to it.
猜猜这是回答和接受,但我只想添加一些更多的细节。
What I did to solve a problem where I would present a login-view as first screen and then wanted to segue to the application if login were correct. I created the segue from the login-view controller to the root view controller and gave it an identifier like "myidentifier".
我做了什么来解决一个问题,我将登录视图显示为第一个屏幕,然后如果登录正确,则想继续应用程序。我创建了从登录视图控制器到根视图控制器的 segue,并给了它一个像“myidentifier”这样的标识符。
Then after checking all login code if the login were correct I'd call
然后在检查所有登录代码后,如果登录正确,我会打电话
[self performSegueWithIdentifier: @"myidentifier" sender: self];
My biggest misunderstanding were that I tried to put the segue on a button and kind of interrupt the segue once it were found.
我最大的误解是我试图将转场放在一个按钮上,一旦找到就中断转场。
回答by Jeff Grimes
You have to link your code to the UIStoryboard
that you're using. Make sure you go into YourViewControllerin your UIStoryboard
, click on the border around it, and then set its identifier
field to a NSString
that you call in your code.
您必须将您的代码链接到UIStoryboard
您正在使用的代码。确保您进入YourViewController中UIStoryboard
,单击它周围的边框,然后将其identifier
字段设置为NSString
您在代码中调用的a 。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
YourViewController *yourViewController =
(YourViewController *)
[storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
[self.navigationController pushViewController:yourViewController animated:YES];
回答by user1723341
For controllers that are in the storyboard.
对于故事板中的控制器。
jhilgert00 is this what you were looking for?
jhilgert00 这就是您要找的吗?
-(IBAction)nav_goHome:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
[self.navigationController pushViewController: myController animated:YES];
}
OR...
或者...
[self performSegueWithIdentifier:@"loginMainSegue" sender:self];
回答by Cameron Lowell Palmer
Storyboard Segues are not to be created outside of the storyboard. You will need to wire it up, despite the drawbacks.
Storyboard Segues 不能在 storyboard 之外创建。尽管存在缺点,但您仍需要将其连接起来。
UIStoryboardSegue Referenceclearly states:
UIStoryboardSegue 参考明确指出:
You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers. You can still initiate a segue programmatically using the performSegueWithIdentifier:sender: method of UIViewController if you want. You might do so to initiate a segue from a source that was added programmatically and therefore not available in Interface Builder.
您不直接创建 segue 对象。相反,当故事板运行时必须在两个视图控制器之间执行转场时,它会创建它们。如果需要,您仍然可以使用 UIViewController 的 performSegueWithIdentifier:sender: 方法以编程方式启动 segue。您可以这样做以从以编程方式添加的源启动 segue,因此在 Interface Builder 中不可用。
You can still programmatically tell the storyboard to present a view controller using a segue using presentModalViewController:
or pushViewController:animated:
calls, but you'll need a storyboard instance.
您仍然可以以编程方式告诉情节提要使用 segue usingpresentModalViewController:
或pushViewController:animated:
call来呈现视图控制器,但您需要一个情节提要实例。
You can call UIStoryboard
s class method to get a named storyboard with bundle nil for the main bundle.
您可以调用UIStoryboard
s 类方法来获取主包的带有包 nil 的命名故事板。
storyboardWithName:bundle:
storyboardWithName:bundle:
回答by Jogi Thakur
well , you can create and also can subclass the UIStoryBoardSegue . subclassing is mostly used for giving custom transition animation.
好吧,您可以创建 UIStoryBoardSegue 也可以创建子类。子类化主要用于提供自定义过渡动画。
you can see video of wwdc 2011 introducing StoryBoard. its available in youtube also.
你可以看到 wwdc 2011 介绍 StoryBoard 的视频。它也可以在 youtube 上找到。
回答by andrewbuilder
I'd like to add a clarification...
我想补充说明...
A common misunderstanding, in fact one that I had for some time, is that a storyboard segue is triggered by the prepareForSegue:sender:
method. It is not. A storyboard segue will perform, regardless of whether you have implemented a prepareForSegue:sender:
method for that (departing from) view controller.
一个常见的误解,实际上是我有一段时间的误解,是故事板转场是由该prepareForSegue:sender:
方法触发的。它不是。无论您是否prepareForSegue:sender:
为该(离开)视图控制器实现了一个方法,故事板 segue 都会执行。
I learnt this from Paul Hegarty's excellent iTunesU lectures. My apologies but unfortunately cannot remember which lecture.
我从 Paul Hegarty 出色的iTunesU 讲座中了解到这一点。我很抱歉,但不幸的是我不记得是哪一堂课了。
If you connect a segue between two view controllers in a storyboard, but do not implement a prepareForSegue:sender:
method, the segue will still segue to the target view controller. It will however segue to that view controller unprepared.
如果您在故事板中的两个视图控制器之间连接了一个 segue,但没有实现prepareForSegue:sender:
方法,segue 仍将 segue 到目标视图控制器。然而,它会在未准备好的情况下转至该视图控制器。
Hope this helps.
希望这可以帮助。
回答by Sanket Chauhan
First of, suppose you have two different views in storyboard, and you want to navigate from one screen to another, so follow this steps:
首先,假设您在故事板中有两个不同的视图,并且您想从一个屏幕导航到另一个屏幕,因此请按照以下步骤操作:
1). Define all your views with class file and also storyboard id in identity inspector.
1)。使用类文件定义所有视图,并在身份检查器中定义故事板 ID。
2). Make sure you add a navigation controller to the first view. Select it in the Storyboard and then Editor >Embed In > Navigation Controller
2)。确保将导航控制器添加到第一个视图。在 Storyboard 中选择它,然后选择 Editor > Embed In > Navigation Controller
3). In your first class, import the "secondClass.h"
3)。在你的第一堂课中,导入“secondClass.h”
#import "ViewController.h
#import "secondController.h"
4). Add this command in the IBAction that has to perform the segue
4)。在必须执行转场的 IBAction 中添加此命令
secondController *next=[self.storyboard instantiateViewControllerWithIdentifier:@"second"];
[self.navigationController pushViewController:next animated:YES];
5). @"second"
is secondview controller class, storyboard id.
5)。@"second"
是 secondview 控制器类,故事板 id。