推送 UIViewController 时实例成员不能用于类型
Instance Member Cannot Be Used on Type when pushing UIViewController
我正在以编程方式设置 ViewController(无故事板)。
我想将数据传递给下一个 VC,虽然我知道如何使用 segue 和情节提要来做到这一点,但我无法弄清楚如何纯粹以编程方式做到这一点。
我收到错误 "Instance Member Cannot Be Used on Type..."
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
CarbonCalculatorResultsViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
是否需要实例化下一个VC才能传递数据?就是这样 但我没有故事板。谢谢!
您应该在 Object
上传递变量,而不是在 Class
上传递变量
替换:CarbonCalculatorResultsViewController.userInformation = userInformation
有:
nextViewController.userInformation = userInformation
注:
CarbonCalculatorResultsViewController
就是 Class
.
nextViewController
就是 Object
.
您的完整代码应如下所示:
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
nextViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
当前示例代码(以上)正在为静态变量(由 CarbonCalculatorResultsViewController.Type
所有。
设置一个值
我认为您要执行的是以下内容:
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
nextViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
此示例代码正在为类型 nextViewController
上的实例变量 userInformation
设置一个值。
第 1 步:设置目的地 class
在 CarbonCalculatorResultsViewController
class 中,声明一个 var
来接收数据,如下所示:
class CarbonCalculatorResultsViewController: UIViewController {
var foo: String? {
didSet {
// What you'd like to do with the data received
print(foo ?? "")
}
}
ovevride func viewDidLoad() {
//
}
}
第 2 步:在源中准备数据 class
let nextViewController = CarbonCalculatorResultsViewController()
// You have access of the variable in CarbonCalculatorResultsViewController
nextViewController.foo = <data_you_want_to_pass>
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
然后,每次 CarbonCalculatorResultsViewController
活跃时,都会调用 foo
的 didSet{}
。
我正在以编程方式设置 ViewController(无故事板)。
我想将数据传递给下一个 VC,虽然我知道如何使用 segue 和情节提要来做到这一点,但我无法弄清楚如何纯粹以编程方式做到这一点。
我收到错误 "Instance Member Cannot Be Used on Type..."
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
CarbonCalculatorResultsViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
是否需要实例化下一个VC才能传递数据?就是这样
您应该在 Object
上传递变量,而不是在 Class
替换:CarbonCalculatorResultsViewController.userInformation = userInformation
有:
nextViewController.userInformation = userInformation
注:
CarbonCalculatorResultsViewController
就是 Class
.
nextViewController
就是 Object
.
您的完整代码应如下所示:
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
nextViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
当前示例代码(以上)正在为静态变量(由 CarbonCalculatorResultsViewController.Type
所有。
我认为您要执行的是以下内容:
// Create Next View Controller Variable
let nextViewController = CarbonCalculatorResultsViewController()
// Pass data to next view controller. There is already a variable in that file: var userInformation: UserInformation?
nextViewController.userInformation = userInformation
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
此示例代码正在为类型 nextViewController
上的实例变量 userInformation
设置一个值。
第 1 步:设置目的地 class
在 CarbonCalculatorResultsViewController
class 中,声明一个 var
来接收数据,如下所示:
class CarbonCalculatorResultsViewController: UIViewController {
var foo: String? {
didSet {
// What you'd like to do with the data received
print(foo ?? "")
}
}
ovevride func viewDidLoad() {
//
}
}
第 2 步:在源中准备数据 class
let nextViewController = CarbonCalculatorResultsViewController()
// You have access of the variable in CarbonCalculatorResultsViewController
nextViewController.foo = <data_you_want_to_pass>
// Push next View Controller
self.navigationController?.pushViewController(nextViewController, animated: true)
然后,每次 CarbonCalculatorResultsViewController
活跃时,都会调用 foo
的 didSet{}
。