Segue使我的程序崩溃.与我的NavigationController和TabBarViewController有关 [英] Segue crashes my program. Has something to do with my NavigationController and my TabBarViewController

查看:91
本文介绍了Segue使我的程序崩溃.与我的NavigationController和TabBarViewController有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试我的应用程序时遇到问题.您可以在下图中看到我如何设置视图:

I encountered a problem while testing my app. You can see how I have setup my views in the following image:

问题是,一切正常.我用我的测试用户登录,下面的代码执行loginSegue,

The thing is that everything works fine. I user my test user to login, and the following code executes the loginSegue,

self.performSegueWithIdentifier("loginSegue", sender: self)

这是一个模式选择,将我的登录名"View Controller"与"Home Tab Bar View Controller"链接在一起.我被重定向到初始Feed视图控制器".一切都很好.但是,当我转到设置视图控制器"并单击注销"按钮时,该按钮具有以下代码(IBAction):

which is a modal segue that links my login "View Controller" with the "Home Tab Bar View Controller". I get redirected to the "Initial Feed View Controller". Everything works great. But when I go to my "Settings View Controller", and click on the Logout button, which has the following code (IBAction):

@IBAction func logoutAction(sender: AnyObject) {

  PFUser.logOut()

  self.performSegueWithIdentifier("logoutSegue", sender: self)
}

我的应用程序崩溃.我的注销按钮的操作具有一定的安全性,可将您带回到登录"View Controller",但先注销用户(顺便说一句,我正在使用Parse).此推送" segue称为logoutSegue.我试图将segue更改为"Popover" segue.那样就解决了这个问题,因为这样做会弄乱我的"Registration View Controller",因为在登录"View Controller"时,我单击了"Register"按钮,并且该应用程序仍然崩溃.

my app crashes. My Logout button's action has a segue that takes you back to the login "View Controller", but logs the user out first (I am using Parse by the way). This "Push" segue is called logoutSegue. I tried to change the segue to be a "Popover" segue. That solves the issue, kinda, since by doing so it messes up my "Registration View Controller" since when in the login "View Controller" I click on the button "Register" and the app crashes anyways.

编译器告诉我,以下代码行是导致错误的代码:

The compiler tells me that the following line of code is the one causing the error:

self.performSegueWithIdentifier("logoutSegue", sender: self)

我不明白为什么会这样.我想这与我的导航控制器"和主页选项卡栏视图控制器"设置有关.也许是与代表们在一起?

I do not understand why this is happening. I suppose that it has something to do with how I have my "Navigation Controller" and "Home Tab Bar View Controller" setup. Or maybe with Delegates?

您能帮我解决这个问题吗?不管您有没有使用Objective-C的方法,请随时提出解决方案,我可以尝试将其从Objective-C转换为Swift.

Could you help me to fix this? It does not matter if you have an Objective-C approach, please feel free to suggest a solution and I can try to convert it from Objective-C to Swift.

除了帮助我解决问题外,我想知道您是否知道Segues(推式,模态,弹出式,替换式)的类型与何时应使用它们之间的区别?我阅读了Apple的文档,但我仍然不完全了解它.

Besides helping me fixing my issue, I was wondering if you know what is the difference between the type of Segues (Push, Modal, Popover, Replace) and when should I use each. I read Apple's documentation but I still don't understand it in its' entirety.

预先感谢您的建议和回应.

Thank you in advance for your advice and responses.

推荐答案

您不想使用传统的segue返回您的登录屏幕.普通的Segue类型总是创建目标视图控制器的新副本.相反,您希望将控制权返回给调用您的视图控制器.

You do not want to use a traditional segue to return to your login screen. The normal segue types always create a new copy of the destination view controller. Instead, you want to return control to the view controller that called you.

您需要在注销时设置放松搜索.就是这样.

You need to set up an unwind segue on logout. Here's how.

1)在ViewController的代码中添加以下功能:

1) In the code for ViewController add this function:

@IBAction func comeHereOnLogout(segue:UIStoryboardSegue) {
    println("Yay, Logged Out!")
}

2)按住Control键并将其从Settings View Controller顶部的view controller图标拖动到 Settings View Controller顶部的exit图标,然后从出现的弹出窗口中选择comeHereOnLogout.

2) Control-drag from the view controller icon at the top of your Settings View Controller to the exit icon at the top of your Settings View Controller and choose comeHereOnLogout from the pop up that appears.

文档大纲中选择该segue,并在 Attributes Inspector 中为其提供一个 Identifier ,例如"logoutSegue".

Select that segue in the Document Outline and give it an Identifier in the Attributes Inspector such as "logoutSegue".

然后,您可以使用以下代码在代码中触发此segue:

Then you can trigger this segue in code with:

self.performSegueWithIdentifier("logoutSegue", sender: self)

3)另外,您也可以将unwind segue注销按钮连接到Settings View Controller顶部的exit图标.在这种情况下,此设置将替换您的注销按钮操作.同样,您希望为该segue提供一个名称,例如"logoutSegue".

3) Alternatively, you can wire the unwind segue from your Logout button to the exit icon at the top of your Settings View Controller. In this case, this segue would replace your logout button action. Again, you'd want to give this segue a name such as "logoutSegue".

在这种情况下,您需要将注销代码准备好进行抵押:

In this case, you'd put your logout code in prepare for segue:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "logoutSegue" {
        PFUser.logOut()
    }
}

这篇关于Segue使我的程序崩溃.与我的NavigationController和TabBarViewController有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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