IOS7,Segue和storyboards - 如何在没有按钮的情况下创建? [英] IOS7, Segue and storyboards - How to create without a button?

查看:119
本文介绍了IOS7,Segue和storyboards - 如何在没有按钮的情况下创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有登录View和Application视图,我已成功在登录视图上实现验证,我需要以成功验证的方式转换到应用程序视图。



我知道我可以在登录按钮上添加一个segue,然后像这样调用它programaticaly ...

  [self performSegueWithIdentifier:@LoginSeguesender:sender]; 

但是,当点击按钮时,这显然会被触发(因为segue是创建附加到按钮)。我刚刚读到我应该创建一个按钮(并隐藏它),然后对segue进行编程调用 - 这看起来有点错误。



如何可以创建一个没有附加到任何特定UI事件的segue吗?

解决方案

删除当前的segue。



附上来自的segue将查看控制器发送到目的地(然后命名)。



现在,按下按钮的方法应如下所示:



Objective-C:



   - (IBAction )validateLogin:(id)sender {
//验证登录
if(validLogin){
[self performSegueWithIdentifier:@mySeguesender:sender];
}
}



Swift:



'预类= 朗 - 迅速prettyprint-越权> @IBAction FUNC validateLogin(发件人:的UIButton){
//验证登录
如果validLogin {
self.performSegueWithIdentifier( mySegue,发送方:发送者)
}
}

这里的关键是原始视图控制器应该是你要创建segue的那个,而不是按钮或任何其他UI元素。



< img src =https://i.stack.imgur.com/vKOMS.pngalt =在此处输入图像说明>



就我个人而言,我挂钩所有我以这种方式进行调整,即使是那些应该在简单按钮上触发的按钮也没有任何验证或逻辑。通过按钮的方法调用它很容易。



而且,我通常在项目的某个地方将所有的segue名称声明为常量字符串。


I currently have a login View and an Application view, I have successfully implemented validation on the login view and I need to programatically shift to the application view on successful validation.

I understand I can add a segue to the login button and then call it programaticaly like so...

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

But this will obviously be triggered when ever 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 programatic call to the segue - this seems a bit 'wrong'.

How can a create a segue that isn't attached to any particular UI event?

解决方案

Delete the current segue.

Attach the segue from the origin view controller to the destination (and then name it).

Now, your button press method should look something like this:

Objective-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.

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.

And, I usually declare all of my segue names as constant strings somewhere in the project.

这篇关于IOS7,Segue和storyboards - 如何在没有按钮的情况下创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆