ios IOS7、Segue 和故事板 - 如何在没有按钮的情况下创建?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21205485/
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
IOS7, Segue and storyboards - How to create without a button?
提问by LiamB
I currently have a login View and an Application view, I have successfully implemented validation on the login view and I need to programmatically shift to the application view on successful validation.
我目前有一个登录视图和一个应用程序视图,我已经成功地在登录视图上实现了验证,我需要在成功验证时以编程方式切换到应用程序视图。
I understand I can add a segueto the login button and then call it programmatically like so...
我知道我可以在登录按钮上添加一个segue,然后像这样以编程方式调用它......
[self performSegueWithIdentifier:@"LoginSegue" sender:sender];
But this will obviously be triggered whenever the button is clicked (As the segue was created attached to the button). I've just read that I should create a button (And hide it) and then make a programmatic call to the segue - this seems a bit 'wrong'.
但这显然会在单击按钮时触发(因为 segue 是附加到按钮上创建的)。我刚刚读到我应该创建一个按钮(并隐藏它),然后对 segue 进行编程调用 - 这似乎有点“错误”。
How can a create a segue that isn't attached to any particular UI event?
如何创建不附加到任何特定 UI 事件的转场?
回答by nhgrif
Delete the current segue.
删除当前的segue。
Attach the segue from the origin view controllerto the destination (and then name it).
将源视图控制器中的 segue 附加到目标(然后命名)。
Now, your button press method should look something like this:
现在,您的按钮按下方法应如下所示:
Objective-C:
目标-C:
- (IBAction)validateLogin:(id)sender {
// validate login
if (validLogin) {
[self performSegueWithIdentifier:@"mySegue" sender:sender];
}
}
Swift:
迅速:
@IBAction func validateLogin(sender: UIButton) {
// validate login
if validLogin {
self.performSegueWithIdentifier("mySegue", sender:sender)
}
}
The key here is that the origin view controller should be the one you're dragging from to create the segue, not the button or any other UI element.
这里的关键是原始视图控制器应该是您要从中拖动以创建转场的控制器,而不是按钮或任何其他 UI 元素。
Personally, I hook ALL of my segues up this way, even the ones that should trigger on simple button pushes without any validation or logic behind them. It's easy enough to call it from the button's method.
就我个人而言,我以这种方式连接我所有的 segue,即使是那些应该在简单的按钮按下时触发的 segues,在它们背后没有任何验证或逻辑。从按钮的方法中调用它很容易。
And, I usually declare all of my segue names as constant strings somewhere in the project.
而且,我通常在项目中的某处将我所有的 segue 名称声明为常量字符串。