ios 使用情节提要的条件转场
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18947328/
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
Conditional Segue Using Storyboard
提问by Saint Robson
I just need to know how to make some 'conditional' redirect / segue to some view. For example :
我只需要知道如何对某些视图进行一些“有条件的”重定向/转场。例如 :
If user haven't login yet, then a view with login form appears. But if user logged in already, they will see their profile view.
如果用户尚未登录,则会出现一个带有登录表单的视图。但是如果用户已经登录,他们将看到他们的个人资料视图。
How to make conditional segue like that using storyboard?
如何使用故事板制作这样的条件转场?
Thank you
谢谢
回答by Gaurav Singh
For conditional segues, don't start a segue from a button as you normally do. Instead of that use the following steps:
对于条件转场,不要像往常一样从按钮开始转场。而不是使用以下步骤:
In your storyboad, start a segue directly from the source view controller to the destination view controller. For doing this, you can drag your source view controller's icon on the bar just below the view to the destination view controller on the main canvas area. Just remember you have to connect two view controllers directly and not with any control.
Enter a suitable segue identifier for this segue. For example say "conditionSegue"
Now, in your source view controller's .m file perform your condition and call
-performSegueWithIdentifier:sender:
method on "self" like this:-(void)loadDestinationVC{ if(condition == YES){ [self performSegueWithIdentifier:@"conditionSegue" sender:nil]; } }
在您的故事板中,直接从源视图控制器到目标视图控制器启动 segue。为此,您可以将视图下方栏上的源视图控制器图标拖动到主画布区域上的目标视图控制器。请记住,您必须直接连接两个视图控制器而不是任何控件。
为此转场输入合适的转场标识符。例如说“conditionSegue”
现在,在您的源视图控制器的 .m 文件中,
-performSegueWithIdentifier:sender:
像这样在“self”上执行您的条件和调用方法:-(void)loadDestinationVC{ if(condition == YES){ [self performSegueWithIdentifier:@"conditionSegue" sender:nil]; } }
I hope I made it clear.
我希望我说清楚了。
回答by Eli Braginskiy
I believe a better approach if you want to use segues from buttons would be to use this:
我相信如果你想使用按钮的 segues 更好的方法是使用这个:
First create a button and use a segue to connect it to another view, give this segue a name
首先创建一个按钮并使用segue将其连接到另一个视图,给这个segue起一个名字
then use this method in your .m file
然后在您的 .m 文件中使用此方法
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([identifier isEqualToString:@"SegueName"]) {
//Check if should make the segue
//if not do something
[self doSomething];
//and return NO;
return NO;
}
return YES;
}
回答by Dashrath
In Swift you could do : as @Gaurav Singh suggested in his answer
在 Swift 中,您可以这样做:正如@Gaurav Singh 在他的回答中所建议的那样
Step 1: Insert a segue from ViewControllerA to ViewControllerB. The segue should start from ViewControllerA itself and not from any control ( like Button ) in it.
步骤 1:从 ViewControllerA 到 ViewControllerB 插入一个 segue。segue 应该从 ViewControllerA 本身开始,而不是从其中的任何控件(如 Button )开始。
Step 2: Give segue an unique identifier in storyboard. Ex: seageFromAtoB
第 2 步:在故事板中为 segue 提供一个唯一标识符。例如:seageFromAtoB
Step 3 : In your ViewControllerforA.swift write :
第 3 步:在您的 ViewControllerforA.swift 中写入:
if(condition == true) {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
If you are performing some task in some other Thread
then using performSegueWithIdentifier
can throw an exception.
如果您在其他任务中执行某些任务,Thread
则使用performSegueWithIdentifier
可能会引发异常。
If you get this error then you should use :
如果您收到此错误,则应使用:
if(condition==true){
NSOperationQueue.mainQueue().addOperationWithBlock {
self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}
}
This will perform segue redirection in main Thread
.
这将在 main 中执行 segue 重定向Thread
。
Hope this help for someone looking for Swift option
希望这对寻找 Swift 选项的人有所帮助
回答by rdelmar
You make the segues directly from one controller to another (or others), and give them identifiers. In code you call choose between segues depending on your conditions, and call performSegueWithIdentifier:sender:.
您可以直接从一个控制器到另一个(或其他)控制器进行转场,并为它们提供标识符。在代码中,您根据自己的条件在 segue 之间进行选择,然后调用 performSegueWithIdentifier:sender:。
回答by user3575114
Create "generic" segue by ctrl dragging from the view controller icon at the bottom to the destination. This segue won't be associated with any action. Then, in your code where ever the event is using your conditional code that you want to trigger the segue, call:
通过 ctrl 从底部的视图控制器图标拖动到目标位置来创建“通用”转场。此 segue 不会与任何操作相关联。然后,在您的代码中,事件正在使用您想要触发 segue 的条件代码,调用:
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender