利用阵列为细分市场做好准备-Xcode 8.0 SWIFT 3.0 [英] Prepare For Segue With Array - Xcode 8.0 Swift 3.0

查看:17
本文介绍了利用阵列为细分市场做好准备-Xcode 8.0 SWIFT 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将项目添加到一个VC上的数组,然后使用"Prepare for Segue"将该数组转移到另一个VC的最佳方法是什么?到目前为止,我的想法是:(Vc1)

var items: [String] = ["Hello"]

(VC2):

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

    var destViewController: ViewController = segue.destination as! ViewController
    destViewController.items = [textField.text!]
    items.append(textField.text!)
}

在VC2上出现一个错误,该行上显示"使用未解析的标识符"

items.append(textField.text!)

推荐答案

我是iOS/SWIFT的新手,但最近也遇到了同样的情况。我是这样做的。

SourceViewController.swft

class SourceViewController: UIViewController {
    let stringToPass = "Hello World"

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! DestinationViewController
        destinationVC.receivedString = stringToPass
        }
    }

DestinationViewController.wift

class DestinationViewController: UIViewController {

    var receivedString: String?

    if let newString = receivedString {
        print(newString)
    }
...

我知道这与您的示例略有不同,但需要注意的重要一点是,当您创建"estinationVC"时,您可以修改它的属性。关键区别在于,在赋值或在您的情况下追加到数组时,您必须提供变量(estinationVC.ReceivedString)的作用域:

destViewController.items.append(textField.text!)

如果不提供作用域,Xcode无法找到您尝试修改的变量(标识符),因为它不是当前文件的一部分,也不是导入的一部分。

这篇关于利用阵列为细分市场做好准备-Xcode 8.0 SWIFT 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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