0

I am android developer and pretty new to iOS.

I have a pageView Controller say A. It is connected to three child View Controller. I want to get back a value from child View Controller back to Root Page View Controller. In android it can be easily done through interface passing and having trouble in doing in iOS with protocols.

I am doing like this

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

I get the error at myViewController.z = self as Cannot assign value of type '(A) -> () -> A' to type 'A.Type'.

I made it work somehow by initializing like this

myViewController.z = A.self

but my app is crashing

Could not cast value of type 'A.Type' (0x1e0d854d8) to 'Z'

hitesh
  • 378
  • 4
  • 12
  • 1
    Hey, you have used the 3rd answer to this link https://stackoverflow.com/questions/39285588/how-to-pass-data-from-child-to-parent-view-controller-in-swift. And also if your problem is not solved, let me know, I will write whole code in the post of this question. – Ashutosh Mishra Jul 22 '20 at 11:43

2 Answers2

0

A few things....

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")
        }
    }
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
0
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")
   }
}
Ashutosh Mishra
  • 1,679
  • 1
  • 13
  • 16