快速关闭视图
问题描述:
在我的HomeViewController中,我有两个segue,一个单击时激活单元格,一个单击条形按钮项目时单击join list
。当单击栏按钮项目时,我想检查用户是否已登录。如果用户没有登录,我想让LoginViewController接口出现,如果他登录了,我想让joinViewController的接口出现。我从参加INTEM到joinViewController按住Ctrl创建从故事板的连接SEGUE,这是我的代码快速关闭视图
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detail" {
var detailScene=segue.destinationViewController as! DetailViewController
if let indexPath=self.table.indexPathForSelectedRow{
let selectedPerson=listPerson[indexPath.row]
detailScene.person=selectedPerson
}}
else
if segue.identifier == "join"{
if !userLoggedIn{
let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController")
self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
}
}
}
一切工作正常,但在LoginViewController我说,IBAction为你关闭该视图
class LoginViewController: UIViewController {
@IBAction func dismissView(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
当按钮被点击时,加载joinViewController而不是homeViewControlled。我做了干净的工作,我该如何解决解雇按钮?
答
如果要动态选择要呈现的segue
,则不应将其设置为UIBarButtonItem
。我的建议是将两个segue
从HomeViewController
拖到LoginViewController
和JoinViewController
,名称为和joinSegue
。
从UIBarButtonItem
创建一个IBAction
到您的HomeViewController
。在代码中,它应该看起来像这样:
@IBAction func barButtonItemPressed(sender: AnyObject) {
if userLoggedIn {
self.performSegueWithIdentifier("joinSegue", sender: self)
} else {
self.performSegueWithIdentifier("loginSegue", sender: self)
}
}
我不确定你在问什么解雇按钮。但是,您不能覆盖prepareForSegue
以选择在此阶段显示哪个UIViewController
,因此它已经出现UIViewController
。
你可以尝试在你的'HomeViewController'中重写'shouldPerformSegueWithIdentifier'并在那里移动登录逻辑。如果未登录,则返回'false',否则返回true。希望这可以帮助。 – riik