xcode 无需故事板即可创建和执行转场

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36216582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:40:14  来源:igfitidea点击:

Create and perform segue without storyboards

iosiphonexcodeswift

提问by EddyW

i got an app without storyboards, all UI creation is made in code and I got a splitViewwhich 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.

我有一个没有故事板的应用程序,所有的 UI 创建都是在代码中创建的,我得到了一个splitView可以在 iPhone 上使用的应用程序,因为该应用程序最初是为 iPad 设计的,所以当你在列表中选择一行时主视图它在 iPhone 上什么都不做,但在 iPad 上运行良好。

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

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

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 :

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

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)
    }

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

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

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

我还尝试调用它segue.perform()并将 performHandler 内容添加到 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

它什么都不做,不会崩溃,只是突出显示我选择的行,仅此而已

Can you guys help me ?

你们能帮帮我吗?

EDIT : As Oleg Gordiichuk said, it's not possible to do what I want to do without Storyboards, so thanks for his help :)

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

采纳答案by Oleg Gordiichuk

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.

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

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

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

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

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.

正如我从我们在评论中的对话中了解到的。您想使用没有故事板的 segues 为 tableview 创建到详细信息视图的导航。现在没有故事板是不可能做到这一点的。

For future learning try to investigate this information.

为了将来的学习,请尝试调查此信息

回答by Mohamad Kaakati

One way is to use the didSelectRowmethod for tableView

一种方法是使用该didSelectRow方法tableView

Swift 3.x

斯威夫特 3.x

// MARK: - Navigation & Pass Data
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("Selected Row \(indexPath.row)")
    let nextVC = YourNextViewController()
    nextVC.YourLabel.text = "Passed Text"
    nextVC.YourLabel.text = YourArray[indexPath.row]

    // Push to next view
    navigationController?.pushViewController(nextVC, animated: true)
}