在 swift 中将值从子视图页面传递到 Pageview 控制器
Pass value from child view page to Pageview controller in swift
我是 android 开发人员,对 iOS 还很陌生。
我有一个 pageView Controller 说 A。它连接到三个子 View Controller。我想从子视图控制器取回一个值返回到根页面视图控制器。
在 android 中,可以通过接口传递轻松完成,但在 iOS 中使用协议会遇到麻烦。
我是这样做的
protocol Z {
func runZ()
}
Class A: UIViewController, Z {
func runZ( withValue value: String)
{
//Perform some function
}
var orderedViewControllers: [UIViewController] = {
var myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myViewController") as! MyViewController
myViewController.z = self
return [ myViewController,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}()
}
Class B : UIViewController{
var z = A.self
func sendBackToA(){
(z as! Z).runZ(withValue : "CustomString")
}
我在 myViewController.z = self 处收到错误
无法将类型“(A) -> () -> A”的值分配给类型 'A.Type'.
我通过像这样初始化使它以某种方式工作
myViewController.z = A.self
但我的应用崩溃了
无法将类型 'A.Type' (0x1e0d854d8) 的值转换为 'Z'
一些事情....
protocol Z {
func runZ(withValue value: String)
}
class A: UIViewController, Z { // "class" rather than "Class"
func runZ( withValue value: String)
{
//Perform some function
}
func orderedViewControllers() -> [UIViewController] { // func because variable declarations don't have a "self"
// Should get rid of the forced unwrap in the next line
let myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myViewController") as! B
myViewController.z = self
return [ myViewController, UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}
}
class B : UIViewController { // "class" rather than "Class"
var z: Z? // The other controller will do the initialization.
func sendBackToA(){
// Verify that the delegate is registered
if let z = z {
z.runZ(withValue : "CustomString")
} else {
print("Missing delegate assignment")
}
}
}
protocol Z:class {
func runZ(withValue value: String?)
}
class A: UIViewController, Z, UINavigationControllerDelegate {
func runZ(withValue value: String?)
{
//Perform some function
}
var orderedViewControllers: [UIViewController] = {
var myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "B") as! B
myViewController.z = self
return [ myViewController,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}()
}
class B : UIViewController{
weak var z:Z?
func sendBackToA(){
z?.runZ(withValue : "CustomString")
}
}
我是 android 开发人员,对 iOS 还很陌生。
我有一个 pageView Controller 说 A。它连接到三个子 View Controller。我想从子视图控制器取回一个值返回到根页面视图控制器。 在 android 中,可以通过接口传递轻松完成,但在 iOS 中使用协议会遇到麻烦。
我是这样做的
protocol Z {
func runZ()
}
Class A: UIViewController, Z {
func runZ( withValue value: String)
{
//Perform some function
}
var orderedViewControllers: [UIViewController] = {
var myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myViewController") as! MyViewController
myViewController.z = self
return [ myViewController,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}()
}
Class B : UIViewController{
var z = A.self
func sendBackToA(){
(z as! Z).runZ(withValue : "CustomString")
}
我在 myViewController.z = self 处收到错误 无法将类型“(A) -> () -> A”的值分配给类型 'A.Type'.
我通过像这样初始化使它以某种方式工作
myViewController.z = A.self
但我的应用崩溃了
无法将类型 'A.Type' (0x1e0d854d8) 的值转换为 'Z'
一些事情....
protocol Z {
func runZ(withValue value: String)
}
class A: UIViewController, Z { // "class" rather than "Class"
func runZ( withValue value: String)
{
//Perform some function
}
func orderedViewControllers() -> [UIViewController] { // func because variable declarations don't have a "self"
// Should get rid of the forced unwrap in the next line
let myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myViewController") as! B
myViewController.z = self
return [ myViewController, UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}
}
class B : UIViewController { // "class" rather than "Class"
var z: Z? // The other controller will do the initialization.
func sendBackToA(){
// Verify that the delegate is registered
if let z = z {
z.runZ(withValue : "CustomString")
} else {
print("Missing delegate assignment")
}
}
}
protocol Z:class {
func runZ(withValue value: String?)
}
class A: UIViewController, Z, UINavigationControllerDelegate {
func runZ(withValue value: String?)
{
//Perform some function
}
var orderedViewControllers: [UIViewController] = {
var myViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "B") as! B
myViewController.z = self
return [ myViewController,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "C")]
}()
}
class B : UIViewController{
weak var z:Z?
func sendBackToA(){
z?.runZ(withValue : "CustomString")
}
}