在没有故事板的情况下创建和执行segue [英] Create and perform segue without storyboards

查看:92
本文介绍了在没有故事板的情况下创建和执行segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有故事板的应用程序,所有UI创建都是在代码中创建的,我得到了一个 splitView 我可以在iPhone上使用它,因为应用程序已经最初仅针对iPad设计,因此当您在主视图中的列表中选择一行时,它在iPhone上无效但在iPad上工作正常。

i got an app without storyboards, all UI creation is made in code and I got a splitView which I would make it usable on iPhone, because as the app as been first designed for iPad only, so that when you select a row in the list in the Master view it does nothing on iPhone but is working fine on iPad.

所以我的问题是否可以创建并执行允许在 didSelectRowAtIndexPath 方法上显示详细信息视图的segue?

So my question is can I create and perform the segue that allows to show the Detail View on the didSelectRowAtIndexPath method ?

以下是我到目前为止所做的事情:

Here's what i've done so far :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!)
        performSegueWithIdentifier("test", sender: self)
    }

但是当运行并选择一行时,应用程序崩溃,告诉它需要一个执行处理程序所以我添加了这个:

but when running and selecting a row the app was crashing telling it needed a performhandler so i added this :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let segue = UIStoryboardSegue(identifier: "test", source: self, destination: detailViewController!, performHandler: { () -> Void in
                let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
                let controller = self.detailViewController!
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true


        })
        performSegueWithIdentifier("test", sender: self)
    }

现在选择行时xcode表示没有segue用这种标识符test。

and now when selecting a row xcode says that there is no segue with such identifier "test".

我还尝试通过 segue.perform()调用它,并将performHandler内容添加到prepareForSegueMethod:

I also tried to call it by segue.perform() and add the performHandler content into the prepareForSegueMethod :

 if segue.identifier == "test" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = self.fetchedResultsController.objectAtIndexPath(indexPath)
                let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
                controller.detailItem = object
                controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
                controller.navigationItem.leftItemsSupplementBackButton = true
            }
        }

并且它什么都不做,不会崩溃,只是突出显示我选择的行,而这一切都是

and it does just nothing, doesn't crash, just highlight the row i selected and that's all

你能帮助我吗?

编辑:正如Oleg Gordiichuk所说,如果没有故事板,就不可能做我想做的事情,所以感谢他的帮助:)

推荐答案

认为它是故事板交互的组成部分,可以从类的名称中理解 UIStoryboardSegue 。以编程方式创建segues是个坏主意。如果我没有犯错误,故事板会为你创建它们。

Segue it is component of the storyboard interaction it is possible to understand from name of the class UIStoryboardSegue. It is bad idea to create segues programmatically. If i am not making mistake storyboard creates them for you.

为了解决你的问题,尝试使用一些常见的方法,比如简单地呈现 ViewController

For solving of you're issue try to use some common ways like simply present ViewController.

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("id") as! MyController
self.presentViewController(vc, animated: true, completion: nil)

as i从评论中的对话中了解。您希望使用没有故事板的segue为tableview创建导航以获取详细信息视图。目前,如果没有故事板,就不可能做到这一点。

As i understand from our conversation in comments. You would like to create navigation for tableview to details view using segues without storyboard. For now it is impossible to do this without storyboard.

为了将来的学习尝试调查这个信息

For future learning try to investigate this information.

这篇关于在没有故事板的情况下创建和执行segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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