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

查看:11
本文介绍了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)

这是一个模态序列,将我的登录视图控制器"与主页选项卡栏视图控制器"链接起来.我被重定向到初始提要视图控制器".一切都很好.但是当我去我的设置视图控制器",然后点击注销按钮,它有以下代码(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)
}

我的应用程序崩溃了.我的注销按钮的操作有一个转场,可将您带回登录视图控制器",但首先将用户注销(顺便说一下,我正在使用 Parse).这种Push"segue 称为 logoutSegue.我试图将 segue 更改为Popover"segue.这解决了这个问题,有点,因为这样做会弄乱我的注册视图控制器",因为当我在登录视图控制器"时单击注册"按钮,应用程序无论如何都会崩溃.

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.

除了帮助我解决问题之外,我想知道您是否知道 Segue 的类型(推送、模态、弹出框、替换)之间的区别以及我应该何时使用它们.我阅读了 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.

您需要在注销时设置 unwind segue.方法如下.

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) 从 Settings View Controller 顶部的 view controller 图标按住 Control 并拖动到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.

Document Outline 中选择该 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) 或者,您可以将 Logout 按钮中的 unwind segue 连接到 顶部的 exit 图标设置视图控制器.在这种情况下,此 segue 将替换您的注销按钮操作.再说一次,你想给这个 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".

在这种情况下,您需要将注销代码放入为 segue 做准备:

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天全站免登陆