ios 为 Swift 中的 Segue 做准备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24040692/
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-08-31 00:26:04  来源:igfitidea点击:

Prepare for Segue in Swift

iosswiftuistoryboarduistoryboardsegue

提问by nikolaus

I'm facing the error message:

我正面临错误消息:

"UIStoryboardSegue does not have a member named 'identifier'"

Here's the code causing the error

这是导致错误的代码

if (segue.identifier == "Load View") {
    // pass data to next view
}

On Obj-C it's fine using like this:

在 Obj-C 上,可以这样使用:

if ([segue.identifier isEqualToString:@"Load View"]) {
   // pass data to next view
}

What am I doing wrong?

我究竟做错了什么?

回答by Cezar

This seems to be due to a problem in the UITableViewControllersubclass template. It comes with a version of the prepareForSeguemethod that would require you to unwrap the segue.

这似乎是由于UITableViewController子类模板中的问题。它带有一个prepareForSegue方法版本,需要您解开 segue。

Replace your current prepareForSeguefunction with:

将您当前的prepareForSegue功能替换为:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        // pass data to next view
    }
}

This version implicitly unwraps the parameters, so you should be fine.

这个版本隐式地解开参数,所以你应该没问题。

回答by Zaid Pathan

Swift 4, Swift 3

斯威夫特 4、斯威夫特 3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "MySegueId" {
        if let nextViewController = segue.destination as? NextViewController {
                nextViewController.valueOfxyz = "XYZ" //Or pass any values
                nextViewController.valueOf123 = 123
        }
    }
}

回答by Ryan Heitner

I think the problem is you have to use the ! to unbundle identifier

我认为问题是你必须使用 ! 解绑标识符

I have

我有

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if segue!.identifier == "Details" {
            let viewController:ViewController = segue!.destinationViewController as ViewController
            let indexPath = self.tableView.indexPathForSelectedRow()
            viewController.pinCode = self.exams[indexPath.row]

        }

    }

My understanding is that without the ! you just get a true or false value

我的理解是,没有!你只是得到一个真值或假值

回答by Sandu

For Swift 2.3,swift3,and swift4:

对于 Swift 2.3、swift3 和 swift4:

Create a perform Segue at didSelectRowAtindexPath

在 didSelectRowAtindexPath 创建执行 Segue

For Ex:

例如:

   self.performSegue(withIdentifier: "uiView", sender: self)

After that Create a prepareforSegue function to catch the Destination segue and pass the value:

之后创建一个 prepareforSegue 函数来捕获 Destination segue 并传递值:

Ex:

前任:

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

       if segue.identifier == "uiView"{

        let destView = segue.destination as! WebViewController
        let indexpath = self.newsTableView.indexPathForSelectedRow
        let indexurl = tableDatalist[(indexpath?.row)!].link
        destView.UrlRec = indexurl

        //let url =

    }
    }

You need to create a variable named UrlRec in Destination ViewController

您需要在 Destination ViewController 中创建一个名为 UrlRec 的变量

回答by Scott Byrns KCL

Swift 1.2

斯威夫特 1.2

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "ShowDeal") {

                if let viewController: DealLandingViewController = segue.destinationViewController as? DealLandingViewController {
                    viewController.dealEntry = deal
                }

            }
     }

回答by M Murteza

Prepare for Segue in Swift 4.2 and Swift 5.

为 Swift 4.2 和 Swift 5 中的 Segue 做准备。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "OrderVC") {
        // pass data to next view
        let viewController = segue.destination as? MyOrderDetailsVC
        viewController!.OrderData = self.MyorderArray[selectedIndex]


    }
}

How to Call segue On specific Event(Like Button Click etc):

如何在特定事件上调用 segue(如按钮单击等):

performSegue(withIdentifier: "OrderVC", sender: self)

回答by alex's

this is one of the ways you can use this function, it is when you want access a variable of another class and change the output based on that variable.

这是您可以使用此函数的一种方式,当您想要访问另一个类的变量并根据该变量更改输出时。

   override func prepare(for segue: UIStoryboardSegue, sender: Any?)  {
        let something = segue.destination as! someViewController
       something.aVariable = anotherVariable
   }

回答by Taylor

Provided you aren't using the same destination view controller with different identifiers, the code can be more concise than the other solutions (and avoids the as!in some of the other answers):

如果您没有使用具有不同标识符的相同目标视图控制器,则代码可以比其他解决方案更简洁(并避免as!其他一些答案中的 ):

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if let myViewController = segue.destinationController as? MyViewController { 
        // Set up the VC
    }
}

回答by Ramiro R. Alcaraz Garcia

Change the segue identifier in the right panel in the section with an id. icon to match the string you used in your conditional.

在带有 id 的部分的右侧面板中更改 segue 标识符。图标以匹配您在条件中使用的字符串。

回答by Jakob Hartman

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if(segue!.identifier){
            var name = segue!.identifier;
            if (name.compare("Load View") == 0){

            }
        }
    }

You can't compare the the identifier with == you have to use the compare() method

您不能将标识符与 == 进行比较,您必须使用 compare() 方法